给定一棵由 0 和 1 组成的二叉树,任务是找到树中任何路径中 1 的最大数量。路径可以在树中的任何节点开始和结束。
示例:
Input:
1
/ \
0 1
/ \
1 1
/ \
1 0
Output: 4
方法:
- 已创建函数countUntil,该函数返回该节点下方任何垂直路径中的最大计数 1。
- 此路径必须是单个路径,并且此路径必须最多包含节点的一个子节点及其本身。即 countUntil 返回左孩子或右孩子的最大计数 1,如果其值为 1,则还将其自身包含在计数中。
- 因此,从任何节点countUntil(node->left) + countUntil(node->right) + node-> value 将给出包含节点及其左右路径且没有节点祖先的路径中的 1 数被认为。
- 取所有节点的最大值将给出所需的答案。
下面是上述方法的实现
C++
// C++ implementation of the above approach
#include
using namespace std;
// A binary tree node
struct Node {
int data;
struct Node *left, *right;
};
// A utility function to allocate a new node
struct Node* newNode(int data)
{
struct Node* newNode = new Node;
newNode->data = data;
newNode->left = newNode->right = NULL;
return (newNode);
}
// This function updates overall count of 1 in 'res'
// And returns count 1s going through root.
int countUntil(Node* root, int& res)
{
// Base Case
if (root == NULL)
return 0;
// l and r store count of 1s going through left and
// right child of root respectively
int l = countUntil(root->left, res);
int r = countUntil(root->right, res);
// maxCount represents the count of 1s when the Node under
// consideration is the root of the maxCount path and no
// ancestors of the root are there in maxCount path
int maxCount;
// if the value at node is 1 then its
// count will be considered
// including the leftCount and the rightCount
if (root->data == 1)
maxCount = l + r + 1;
else
maxCount = l + r;
// Store the Maximum Result.
res = max(res, maxCount);
// return max count in a single path.
// This path must include at-most one child
// of the root as well as itself
// if the value at node is 1
// then its count will be considered
// including the maximum of leftCount or the rightCount
if (root->data == 1)
return max(l, r) + 1;
else
return max(l, r);
}
// Returns maximum count of 1 in any path
// in tree with given root
int findMaxCount(Node* root)
{
// Initialize result
int res = INT_MIN;
// Compute and return result
countUntil(root, res);
return res;
}
// Driver program
int main(void)
{
struct Node* root = newNode(1);
root->left = newNode(0);
root->right = newNode(1);
root->left->left = newNode(1);
root->left->right = newNode(1);
root->left->right->left = newNode(1);
root->left->right->right = newNode(0);
cout << findMaxCount(root);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// A binary tree node
static class Node
{
int data;
Node left, right;
};
static int res;
// A utility function to allocate a new node
static Node newNode(int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.left = newNode.right = null;
return (newNode);
}
// This function updates overall count of 1 in 'res'
// And returns count 1s going through root.
static int countUntil(Node root)
{
// Base Case
if (root == null)
return 0;
// l and r store count of 1s going through left and
// right child of root respectively
int l = countUntil(root.left);
int r = countUntil(root.right);
// maxCount represents the count of 1s when the Node under
// consideration is the root of the maxCount path and no
// ancestors of the root are there in maxCount path
int maxCount;
// if the value at node is 1 then its
// count will be considered
// including the leftCount and the rightCount
if (root.data == 1)
maxCount = l + r + 1;
else
maxCount = l + r;
// Store the Maximum Result.
res = Math.max(res, maxCount);
// return max count in a single path.
// This path must include at-most one child
// of the root as well as itself
// if the value at node is 1
// then its count will be considered
// including the maximum of leftCount or the rightCount
if (root.data == 1)
return Math.max(l, r) + 1;
else
return Math.max(l, r);
}
// Returns maximum count of 1 in any path
// in tree with given root
static int findMaxCount(Node root)
{
// Initialize result
res = Integer.MIN_VALUE;
// Compute and return result
countUntil(root);
return res;
}
// Driver program
public static void main(String[] args)
{
Node root = newNode(1);
root.left = newNode(0);
root.right = newNode(1);
root.left.left = newNode(1);
root.left.right = newNode(1);
root.left.right.left = newNode(1);
root.left.right.right = newNode(0);
System.out.print(findMaxCount(root));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python implementation of the above approach
# A binary tree node
class Node:
def __init__(self):
self.data = 0
self.left = None
self.right = None
# A utility function to allocate a new node
def newNode(data):
newNode = Node()
newNode.data = data
newNode.left = newNode.right = None
return (newNode)
res = 0
# This function updates overall count of 1 in 'res'
# And returns count 1s going through root.
def countUntil( root):
global res
# Base Case
if (root == None):
return 0
# l and r store count of 1s going through left and
# right child of root respectively
l = countUntil(root.left)
r = countUntil(root.right)
# maxCount represents the count of 1s when the Node under
# consideration is the root of the maxCount path and no
# ancestors of the root are there in maxCount path
maxCount = 0
# if the value at node is 1 then its
# count will be considered
# including the leftCount and the rightCount
if (root.data == 1):
maxCount = l + r + 1
else:
maxCount = l + r
# Store the Maximum Result.
res = max(res, maxCount)
# return max count in a single path.
# This path must include at-most one child
# of the root as well as itself
# if the value at node is 1
# then its count will be considered
# including the maximum of leftCount or the rightCount
if (root.data == 1):
return max(l, r) + 1
else:
return max(l, r)
# Returns maximum count of 1 in any path
# in tree with given root
def findMaxCount(root):
global res
# Initialize result
res = -999999
# Compute and return result
countUntil(root)
return res
# Driver program
root = newNode(1)
root.left = newNode(0)
root.right = newNode(1)
root.left.left = newNode(1)
root.left.right = newNode(1)
root.left.right.left = newNode(1)
root.left.right.right = newNode(0)
print(findMaxCount(root))
# This code is contributed by Arnab Kundu
C#
// C# implementation of the above approach
using System;
class GFG
{
// A binary tree node
class Node
{
public int data;
public Node left, right;
};
static int res;
// A utility function to allocate a new node
static Node newNode(int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.left = newNode.right = null;
return (newNode);
}
// This function updates overall count of 1 in 'res'
// And returns count 1s going through root.
static int countUntil(Node root)
{
// Base Case
if (root == null)
return 0;
// l and r store count of 1s going through left and
// right child of root respectively
int l = countUntil(root.left);
int r = countUntil(root.right);
// maxCount represents the count of 1s when the Node under
// consideration is the root of the maxCount path and no
// ancestors of the root are there in maxCount path
int maxCount;
// if the value at node is 1 then its
// count will be considered
// including the leftCount and the rightCount
if (root.data == 1)
maxCount = l + r + 1;
else
maxCount = l + r;
// Store the Maximum Result.
res = Math.Max(res, maxCount);
// return max count in a single path.
// This path must include at-most one child
// of the root as well as itself
// if the value at node is 1
// then its count will be considered
// including the maximum of leftCount or the rightCount
if (root.data == 1)
return Math.Max(l, r) + 1;
else
return Math.Max(l, r);
}
// Returns maximum count of 1 in any path
// in tree with given root
static int findMaxCount(Node root)
{
// Initialize result
res = int.MinValue;
// Compute and return result
countUntil(root);
return res;
}
// Driver program
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(0);
root.right = newNode(1);
root.left.left = newNode(1);
root.left.right = newNode(1);
root.left.right.left = newNode(1);
root.left.right.right = newNode(0);
Console.Write(findMaxCount(root));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
4
时间复杂度: O(n)
其中 n 是二叉树中的节点数。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。