-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaximum width of binary tree-Leetcode-AnujSoni.cpp
42 lines (42 loc) · 1.26 KB
/
Maximum width of binary tree-Leetcode-AnujSoni.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
if(!root)
return 0;
queue<pair<TreeNode*,int>> q;
q.push({root,1});
int ans = 1;
while(!q.empty()){
int s = q.size();
int minval = INT_MAX, maxval = 0;
while(s--){
pair<TreeNode*,int> p = q.front();q.pop();
TreeNode* temp = p.first;
int t = p.second;
if(temp->left){
q.push({temp->left,2*t});
minval = min(minval,2*t);
maxval = max(maxval,2*t);
}
if(temp->right){
q.push({temp->right,2*t-1});
minval = min(minval,2*t-1);
maxval = max(maxval,2*t-1);
}
}
ans = max(ans,maxval-minval+1);
}
return ans;
}
};