-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path662.cpp
More file actions
26 lines (26 loc) · 828 Bytes
/
662.cpp
File metadata and controls
26 lines (26 loc) · 828 Bytes
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
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
if(root == nullptr)
return 0;
queue<pair<TreeNode*, int>> q;
q.push({root, 1});
long long res = 0;
while(!q.empty()) {
long long left_most = q.front().second;
long long right_most = left_most;
int size = q.size();
while(size--){
TreeNode* curr = q.front().first;
right_most = q.front().second;
q.pop();
if(curr->left != NULL)
q.push({curr->left, 2 * right_most});
if(curr->right != NULL)
q.push({curr->right, 2 * right_most + 1});
}
res = max(res, right_most - left_most + 1);
}
return res;
}
};