-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathBST3.cpp
37 lines (23 loc) · 921 Bytes
/
BST3.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
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
queue<pair< TreeNode* , int > > q;
q.push( { root , 0 } );
int ans =0;
while( !q.empty()){
int sz = q.size();
int left = INT_MAX;
while(sz--){
auto top= q.front();
q.pop();
if ( left == INT_MAX)
left = top.second;
if ( top.first ->left)
q.push({ top.first ->left , (top.second - left) * 2 });
if ( top.first -> right)
q.push({ top.first ->right , (top.second - left) * 2 + 1 });
ans = max( ans , top.second - left);
}
}
return ans +1 ;
}