Skip to content

Commit

Permalink
Time: 8 ms (77.53%), Space: 17.4 MB (25.97%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Apr 20, 2023
1 parent d02114c commit a92e81b
Showing 1 changed file with 31 additions and 0 deletions.
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;


}
};

0 comments on commit a92e81b

Please sign in to comment.