-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 8 ms (77.53%), Space: 17.4 MB (25.97%) - LeetHub
- Loading branch information
1 parent
d02114c
commit a92e81b
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
0662-maximum-width-of-binary-tree/0662-maximum-width-of-binary-tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution { | ||
public: | ||
typedef unsigned long long ll; | ||
int widthOfBinaryTree(TreeNode* root) { | ||
queue<pair<TreeNode*,ll>> q; | ||
q.push({root,0}); | ||
ll ans=0; | ||
while(!q.empty()) | ||
{ | ||
ll size=q.size(); | ||
ll n=q.front().second; | ||
ll m=q.back().second; | ||
ans=max(ans,m-n+1); | ||
|
||
for(ll i=0;i<size;i++) | ||
{ | ||
TreeNode* node=q.front().first; | ||
ll ind=q.front().second; | ||
|
||
q.pop(); | ||
if(node->left!=NULL) | ||
q.push({node->left,2*ind+1}); | ||
if(node->right!=NULL) | ||
q.push({node->right,2*ind+2}); | ||
} | ||
} | ||
return ans; | ||
|
||
|
||
} | ||
}; |