-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum-width-of-Binary-tree.java
54 lines (50 loc) · 1.38 KB
/
Maximum-width-of-Binary-tree.java
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
class MyNode{
TreeNode node;
int idx;
MyNode(TreeNode node, int idx){
this.node = node;
this.idx = idx;
}
}
public int widthOfBinaryTree(TreeNode root) {
int max = 0;
if(root == null) return max;
Queue<MyNode> q = new LinkedList<>();
q.add(new MyNode(root, 0));
while(!q.isEmpty()){
int size = q.size();
int start = 0;
int end = 0;
for(int i = 0; i < size; i++){
MyNode p = q.poll();
int index = p.idx;
if(i == 0) start = index;
if(i == size-1) end = index;
if(p.node.left != null){
q.add(new MyNode(p.node.left, index*2 + 1));
}
if(p.node.right != null){
q.add(new MyNode(p.node.right, index*2 + 2));
}
}
max = Math.max(max, end - start + 1);
}
return max;
}
}