File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments