Skip to content

Commit a5e08f3

Browse files
Merge pull request #335 from Dhruvverma2020/patch-1
Create Maximum width of tree
2 parents ce1099d + a1a4023 commit a5e08f3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Trees/Maximum width of tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
// Function to get the maximum width of a binary tree.
4+
int getMaxWidth(Node* root)
5+
{
6+
if(root==NULL)
7+
{
8+
return 0;
9+
}
10+
queue<Node*>q;
11+
q.push(root);
12+
int res=0;
13+
while(q.empty()==false)
14+
{
15+
int curr=q.size();
16+
res=max(curr,res);
17+
for(int i=0;i<curr;i++)
18+
{
19+
Node*curr=q.front();
20+
q.pop();
21+
22+
if(curr->left!=NULL)
23+
{
24+
q.push(curr->left);
25+
}
26+
if(curr->right!=NULL)
27+
{
28+
q.push(curr->right);
29+
}
30+
}
31+
}
32+
return res;
33+
// Your code here
34+
}
35+
};

0 commit comments

Comments
 (0)