-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Tree_18.cpp
More file actions
69 lines (54 loc) · 1.43 KB
/
Copy pathBinary_Tree_18.cpp
File metadata and controls
69 lines (54 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Maximum Width of Binary Tree
#include<iostream>
#include<queue>
using namespace std;
class TreeNode{
public:
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int data){
val=data;
left=NULL;
right=NULL;
}
};
int Width_of_Binary_Tree(TreeNode* Root){
int width=0;
queue<pair<TreeNode*,unsigned long long>> q;
q.push({Root,0});
while(q.size()>0){
int curr_size = q.size();
unsigned long long startIdx=q.front().second;
unsigned long long endIdx=q.back().second;
width = max(width , (int)(endIdx-startIdx+1));
for(int i=0;i<curr_size;i++){
auto curr = q.front();
q.pop();
if(curr.first->left){
q.push({curr.first->left,curr.second*2 + 1});
}
if(curr.first->right){
q.push({curr.first->right,curr.second*2 + 2});
}
}
}
return width;
}
int main() {
/* Constructing the following tree:
1
/ \
3 2
/ \ \
5 3 9
*/
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(3);
root->right = new TreeNode(2);
root->left->left = new TreeNode(5);
root->left->right = new TreeNode(3);
root->right->right = new TreeNode(9);
cout << "Maximum Width of Binary Tree: " << Width_of_Binary_Tree(root) << endl;
return 0;
}