Skip to content

Commit c149ee2

Browse files
authored
Merge pull request #772 from ABHIT33/main
Maximum Node Level.cpp
2 parents 8616f61 + c473528 commit c149ee2

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

binary-tree/Maximum Node Level.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node {
5+
int data;
6+
Node *left;
7+
Node *right;
8+
9+
Node(int val) {
10+
data = val;
11+
left = right = NULL;
12+
}
13+
};
14+
// Function to Build Tree
15+
Node* buildTree(string str)
16+
{
17+
// Corner Case
18+
if(str.length() == 0 || str[0] == 'N')
19+
return NULL;
20+
21+
// Creating vector of strings from input
22+
// string after spliting by space
23+
vector<string> ip;
24+
25+
istringstream iss(str);
26+
for(string str; iss >> str; )
27+
ip.push_back(str);
28+
29+
// Create the root of the tree
30+
Node* root = new Node(stoi(ip[0]));
31+
32+
// Push the root to the queue
33+
queue<Node*> queue;
34+
queue.push(root);
35+
36+
// Starting from the second element
37+
int i = 1;
38+
while(!queue.empty() && i < ip.size()) {
39+
40+
// Get and remove the front of the queue
41+
Node* currNode = queue.front();
42+
queue.pop();
43+
44+
// Get the current node's value from the string
45+
string currVal = ip[i];
46+
47+
// If the left child is not null
48+
if(currVal != "N") {
49+
50+
// Create the left child for the current node
51+
currNode->left = new Node(stoi(currVal));
52+
53+
// Push it to the queue
54+
queue.push(currNode->left);
55+
}
56+
57+
// For the right child
58+
i++;
59+
if(i >= ip.size())
60+
break;
61+
currVal = ip[i];
62+
63+
// If the right child is not null
64+
if(currVal != "N") {
65+
66+
// Create the right child for the current node
67+
currNode->right = new Node(stoi(currVal));
68+
69+
// Push it to the queue
70+
queue.push(currNode->right);
71+
}
72+
i++;
73+
}
74+
75+
return root;
76+
}
77+
78+
int maxNodeLevel(Node *root);
79+
80+
int main()
81+
{
82+
83+
int t;
84+
scanf("%d ",&t);
85+
while(t--)
86+
{
87+
string s;
88+
getline(cin,s);
89+
Node* root = buildTree(s);
90+
int ans = maxNodeLevel(root);
91+
cout<<ans<<endl;
92+
}
93+
return 1;
94+
}
95+
// } Driver Code Ends
96+
97+
98+
/*Complete the function below
99+
Node is as follows:
100+
struct Node {
101+
int data;
102+
Node *left;
103+
Node *right;
104+
105+
Node(int val) {
106+
data = val;
107+
left = right = NULL;
108+
}
109+
};
110+
*/
111+
112+
// Return the level (0-indexed) with maximum number of nodes.
113+
int maxNodeLevel(Node *root)
114+
{
115+
// Add your code here
116+
if(root==NULL)
117+
return 0;
118+
queue<Node *> q;
119+
q.push(root);
120+
int m=0;
121+
int level=-1;
122+
int max_level=0;
123+
while(q.empty()==false)
124+
{
125+
level++;
126+
int count=q.size();
127+
if(count>m)
128+
{
129+
m=count;
130+
max_level=level;
131+
}
132+
for(int i=0;i<count;i++)
133+
{
134+
Node* cur=q.front();
135+
q.pop();
136+
if(cur->left!=NULL)
137+
q.push(cur->left);
138+
if(cur->right!=NULL)
139+
q.push(cur->right);
140+
}
141+
}
142+
return max_level;
143+
144+
}

0 commit comments

Comments
 (0)