Skip to content

Commit 4ad01fd

Browse files
authored
Update 662.Maximum-Width-of-Binary-Tree.cpp
1 parent 03740e8 commit 4ad01fd

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

Tree/662.Maximum-Width-of-Binary-Tree/662.Maximum-Width-of-Binary-Tree.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,25 @@ class Solution {
2121
int len = q.size();
2222
ans = max(ans, q.back()->val - q.front()->val + 1);
2323

24-
int flag = (len == 1);
24+
int base = q.front()->val;
2525

2626
while (len--)
2727
{
2828
TreeNode* node = q.front();
2929
q.pop_front();
30-
31-
if (flag==1) node->val = 0;
3230

3331
if (node->left)
3432
{
35-
node->left->val = node->val*2+1;
33+
node->left->val = (node->val-base)*2+1;
3634
q.push_back(node->left);
3735
}
3836
if (node->right)
3937
{
40-
node->right->val = node->val*2+2;
38+
node->right->val = (node->val-base)*2+2;
4139
q.push_back(node->right);
4240
}
4341
}
4442
}
4543
return ans;
4644
}
47-
48-
4945
};

0 commit comments

Comments
 (0)