-
Notifications
You must be signed in to change notification settings - Fork 5
/
q1.cpp
163 lines (132 loc) · 2.8 KB
/
q1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
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<int>& 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<int> 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);
}