Skip to content

Commit e9831c2

Browse files
committed
Maximum width of tree.cpp
1 parent c69cb76 commit e9831c2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Maximum width of tree.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
int getMaxWidth(Node* root){
2+
if(root==NULL){
3+
return 0;
4+
}
5+
priority_queue<int>pq;
6+
queue<Node *>Q;
7+
Q.push(root);
8+
while(!Q.empty()){
9+
int size=Q.size();
10+
pq.push(size);
11+
for(int i=0;i<size;i++){
12+
Node * top=Q.front();
13+
Q.pop();
14+
if(top->left){
15+
Q.push(top->left);
16+
}
17+
if(top->right){
18+
Q.push(top->right);
19+
}
20+
}
21+
}
22+
return pq.top();
23+
}

0 commit comments

Comments
 (0)