给定一个二叉树,任务是打印所有可能的根到叶路径,这些路径具有最大数量的偶数节点。
例子:
Input:
Output:
2 -> 6 -> 4 -> 10
2 -> 6 -> 4 -> 12
Explanation:
Count of even nodes on the path 2 -> 6 -> 4 -> 10 = 4
Count of even nodes on the path 2 -> 6 -> 4 -> 12 = 4
Count of even nodes on the path 2 -> 6 -> 7 -> 1 = 2
Count of even nodes on the path 2 -> 3 -> 11 = 1
Therefore, the paths consisting of maximum count of even nodes are {2 -> 6 -> 4 -> 10} and {2 -> 6 -> 4 -> 12 }.
Input:
Output: 5 -> 6 -> 4.
方法:该问题可以使用 Preorder Traversal 来解决。这个想法是遍历给定的二叉树并找到根到叶路径中可能的偶数节点的最大数量。最后,遍历树并打印具有最大偶数节点数的所有可能的根到叶路径。请按照以下步骤解决问题:
- 初始化两个变量,比如cntMaxEven和cntEven,以存储从根到叶节点的偶数节点的最大计数和从根到叶节点的偶数节点的计数。
- 遍历树并检查以下条件:
- 检查当前节点是否为叶节点并且cntEven > cntMaxEven与否。如果发现为真,则更新cntMaxEven = cntEven 。
- 否则,检查当前节点是否为偶数节点。如果发现为真,则更新cntEven += 1 。
- 最后,遍历树并打印所有可能的根到叶路径,其偶数节点数等于cntMaxEven 。
C++
2
/ \
6 3
/ \ \
4 7 11
/ \ \
10 12 1
Java
5
/ \
6 3
/ \ \
4 9 2
Python3
// C++ program to implement
// the above approach
#include
using namespace std;
// Structure of the binary tree
struct Node {
// Stores data
int data;
// Stores left child
Node* left;
// Stores right child
Node* right;
// Initialize a node
// of binary tree
Node(int val)
{
// Update data;
data = val;
left = right = NULL;
}
};
// Function to find maximum count
// of even nodes in a path
int cntMaxEvenNodes(Node* root)
{
// If the tree is an
// empty binary tree.
if (root == NULL) {
return 0;
}
// Stores count of even nodes
// in current subtree
int cntEven = 0;
// If root node is
// an even node
if (root->data % 2
== 0) {
// Update cntEven
cntEven += 1;
}
// Stores count of even nodes
// in left subtree.
int X = cntMaxEvenNodes(
root->left);
// Stores count of even nodes
// in right subtree.
int Y = cntMaxEvenNodes(
root->right);
// cntEven
cntEven += max(X, Y);
return cntEven;
}
// Function to print paths having
// count of even nodes equal
// to maximum count of even nodes
void printPath(Node* root, int cntEven,
int cntMaxEven,
vector& path)
{
// If the tree is an
// empty Binary Tree
if (root == NULL) {
return;
}
// If current node value is even
if (root->data % 2 == 0) {
path.push_back(
root->data);
cntEven += 1;
}
// If current node is
// a leaf node
if (root->left == NULL
&& root->right == NULL) {
// If count of even nodes in
// path is equal to cntMaxEven
if (cntEven == cntMaxEven) {
// Stores length of path
int N = path.size();
// Print path
for (int i = 0; i < N - 1;
i++) {
cout << path[i] << " -> ";
}
cout << path[N - 1] << endl;
}
}
// Left subtree
printPath(root->left, cntEven,
cntMaxEven, path);
// Right subtree
printPath(root->right, cntEven,
cntMaxEven, path);
// If current node is even
if (root->data % 2 == 0) {
path.pop_back();
// Update cntEven
cntEven--;
}
}
// Utility Function to print path
// from root to leaf node having
// maximum count of even nodes
void printMaxPath(Node* root)
{
// Stores maximum count of even
// nodes of a path in the tree
int cntMaxEven;
cntMaxEven
= cntMaxEvenNodes(root);
// Stores path of tree having
// even nodes
vector path;
printPath(root, 0, cntMaxEven,
path);
}
// Driver code
int main()
{
// Create tree.
Node* root = NULL;
root = new Node(2);
root->left = new Node(6);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(7);
root->right->right = new Node(11);
root->left->left->left = new Node(10);
root->left->left->right = new Node(12);
root->left->right->right = new Node(1);
printMaxPath(root);
}
C#
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Structure of the binary tree
static class Node
{
// Stores data
int data;
// Stores left child
Node left;
// Stores right child
Node right;
// Initialize a node
// of binary tree
Node(int val)
{
// Update data;
data = val;
left = right = null;
}
};
// Function to find maximum count
// of even nodes in a path
static int cntMaxEvenNodes(Node root)
{
// If the tree is an
// empty binary tree.
if (root == null)
{
return 0;
}
// Stores count of even nodes
// in current subtree
int cntEven = 0;
// If root node is
// an even node
if (root.data % 2 == 0)
{
// Update cntEven
cntEven += 1;
}
// Stores count of even nodes
// in left subtree.
int X = cntMaxEvenNodes(root.left);
// Stores count of even nodes
// in right subtree.
int Y = cntMaxEvenNodes(root.right);
// cntEven
cntEven += Math.max(X, Y);
return cntEven;
}
// Function to print paths having
// count of even nodes equal
// to maximum count of even nodes
static void printPath(Node root, int cntEven,
int cntMaxEven,
Vector path)
{
// If the tree is an
// empty Binary Tree
if (root == null)
{
return;
}
// If current node value is even
if (root.data % 2 == 0)
{
path.add(root.data);
cntEven += 1;
}
// If current node is
// a leaf node
if (root.left == null &&
root.right == null)
{
// If count of even nodes in
// path is equal to cntMaxEven
if (cntEven == cntMaxEven)
{
// Stores length of path
int N = path.size();
// Print path
for(int i = 0; i < N - 1; i++)
{
System.out.print(path.get(i) + " -> ");
}
System.out.print(path.get(N - 1) + "\n");
}
}
// Left subtree
printPath(root.left, cntEven,
cntMaxEven, path);
// Right subtree
printPath(root.right, cntEven,
cntMaxEven, path);
// If current node is even
if (root.data % 2 == 0)
{
path.remove(path.size() - 1);
// Update cntEven
cntEven--;
}
}
// Utility Function to print path
// from root to leaf node having
// maximum count of even nodes
static void printMaxPath(Node root)
{
// Stores maximum count of even
// nodes of a path in the tree
int cntMaxEven;
cntMaxEven = cntMaxEvenNodes(root);
// Stores path of tree having
// even nodes
Vector path = new Vector<>();
printPath(root, 0, cntMaxEven,
path);
}
// Driver code
public static void main(String[] args)
{
// Create tree.
Node root = null;
root = new Node(2);
root.left = new Node(6);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(7);
root.right.right = new Node(11);
root.left.left.left = new Node(10);
root.left.left.right = new Node(12);
root.left.right.right = new Node(1);
printMaxPath(root);
}
}
// This code is contributed by Amit Katiyar
# Python3 program to implement
# the above approach
# Structure of the binary tree
class newNode:
def __init__(self, val):
self.data = val
self.left = None
self.right = None
path = []
# Function to find maximum count
# of even nodes in a path
def cntMaxEvenNodes(root):
# If the tree is an
# empty binary tree.
if (root == None):
return 0
# Stores count of even nodes
# in current subtree
cntEven = 0
# If root node is
# an even node
if (root.data % 2 == 0):
# Update cntEven
cntEven += 1
# Stores count of even nodes
# in left subtree.
X = cntMaxEvenNodes(root.left)
# Stores count of even nodes
# in right subtree.
Y = cntMaxEvenNodes(root.right)
# cntEven
cntEven += max(X, Y)
return cntEven
# Function to print paths having
# count of even nodes equal
# to maximum count of even nodes
def printPath(root, cntEven, cntMaxEven):
global path
# If the tree is an
# empty Binary Tree
if (root == None):
return
# If current node value is even
if (root.data % 2 == 0):
path.append(root.data)
cntEven += 1
# If current node is
# a leaf node
if (root.left == None and
root.right == None):
# If count of even nodes in
# path is equal to cntMaxEven
if (cntEven == cntMaxEven):
# Stores length of path
N = len(path)
# Print path
for i in range(N - 1):
print(path[i], end = "->")
print(path[N - 1])
# Left subtree
printPath(root.left, cntEven, cntMaxEven)
# Right subtree
printPath(root.right, cntEven, cntMaxEven)
# If current node is even
if (root.data % 2 == 0):
path.remove(path[len(path) - 1])
# Update cntEven
cntEven -= 1
# Utility Function to print path
# from root to leaf node having
# maximum count of even nodes
def printMaxPath(root):
global path
# Stores maximum count of even
# nodes of a path in the tree
cntMaxEven = cntMaxEvenNodes(root)
printPath(root, 0, cntMaxEven)
# Driver code
if __name__ == '__main__':
# Create tree.
root = None
root = newNode(2)
root.left = newNode(6)
root.right = newNode(3)
root.left.left = newNode(4)
root.left.right = newNode(7)
root.right.right = newNode(11)
root.left.left.left = newNode(10)
root.left.left.right = newNode(12)
root.left.right.right = newNode(1)
printMaxPath(root)
# This code is contributed by bgangwar59
时间复杂度: O(N),其中 N 是二叉树中的节点数。
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live