-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path662.cpp
More file actions
38 lines (36 loc) · 1.13 KB
/
Copy path662.cpp
File metadata and controls
38 lines (36 loc) · 1.13 KB
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
class Solution {
public:
struct PosNode {
TreeNode *node;
unsigned long pos;
PosNode(TreeNode *node, unsigned long pos) : node(node), pos(pos) {}
};
int widthOfBinaryTree(TreeNode *root) {
if (!root)
return 0;
queue<PosNode *> que;
unsigned long max_width = 1, left = 0, right = 0;
auto size = 1;
que.push(new PosNode(root, 1));
while (!que.empty()) {
PosNode *curr = que.front();
que.pop();
--size;
if (curr) {
if (left == 0) {
left = curr->pos;
} else
right = curr->pos;
if (curr->node->left) que.push(new PosNode(curr->node->left, curr->pos * 2));
if (curr->node->right) que.push(new PosNode(curr->node->right, curr->pos * 2 + 1));
}
if (size == 0) {
size = que.size();
max_width = max(max_width, right > left ? right - left + 1 : 0);
left = 0;
right = 0;
}
}
return max_width;
}
};