-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0662.cpp
More file actions
25 lines (21 loc) · 714 Bytes
/
Copy path0662.cpp
File metadata and controls
25 lines (21 loc) · 714 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
class Solution {
public:
int widthOfBinaryTree(TreeNode *root) {
if (root == NULL) return 0;
queue<pair<TreeNode *, int>> q;
int res = 1;
q.push({root, 0});
while (!q.empty()) {
int start = q.front().second, end = q.back().second;
res = max(res, end - start + 1);
for (int k = q.size(); k > 0; k--) {
pair<TreeNode *, int> p = q.front();
q.pop();
int idx = p.second - start;
if (p.first->left) q.push({p.first->left, 2ll * idx + 1});
if (p.first->right) q.push({p.first->right, 2ll * idx + 2});
}
}
return res;
}
};