Skip to content

Commit 8c253c3

Browse files
authored
Merge pull request larissalages#49 from partha-sarathi-yogi/master
Solution for leetcode #662
2 parents 0095ee3 + d294b70 commit 8c253c3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

leetcode/cpp/Tree/662.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This is the solution for Leetcode #662
2+
// link of the question is: https://leetcode.com/problems/maximum-width-of-binary-tree/
3+
4+
// Solution class begins here
5+
class Solution {
6+
public:
7+
// uint64_t is 64 bit unsigned integer
8+
uint64_t widthOfBinaryTree(TreeNode* root) {
9+
if(root==NULL) return 0;
10+
queue <pair<TreeNode *, uint64_t>> q;
11+
q.push(make_pair(root, 0));
12+
uint64_t res=0;
13+
uint64_t index=0;
14+
pair<TreeNode*, uint64_t> f;
15+
pair<TreeNode*, uint64_t> curr;
16+
while(!q.empty()){
17+
f = q.front();
18+
uint64_t count = q.size();
19+
while(count--){
20+
curr = q.front();
21+
q.pop();
22+
TreeNode *node = curr.first;
23+
index = curr.second;
24+
if(node->left!=NULL) q.push(make_pair(node->left, 2*index));
25+
if(node->right!=NULL) q.push(make_pair(node->right, 2*index + 1));
26+
}
27+
res = max(res, curr.second - f.second + 1);
28+
}
29+
return res;
30+
}
31+
};

0 commit comments

Comments
 (0)