-
Notifications
You must be signed in to change notification settings - Fork 2
/
MaximumWidthBinaryTree.java
65 lines (50 loc) · 1.65 KB
/
MaximumWidthBinaryTree.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
55
56
57
58
59
60
61
62
63
64
65
package Leetcode;
import java.util.LinkedList;
import java.util.Queue;
public class MaximumWidthBinaryTree {
public static int widthOfBinaryTree(TreeNode root) {
int result = 0;
Queue<Pair> queue = new LinkedList<>();
if(root == null)
return 0;
queue.offer(new Pair(root, 1));
while(!queue.isEmpty()) {
int len = queue.size();
int start = 0;
int end = 0;
for(int i = 0; i < len; i++) {
Pair temp = queue.poll();
TreeNode current = temp.node;
int index = temp.index;
if(i == 0)
start = index;
if(i == len - 1)
end = index;
if(current.left != null)
queue.offer(new Pair(current.left, 2*index));
if(current.right != null)
queue.offer(new Pair(current.right, 2*index+1));
}
result = Math.max(result, end - start + 1);
}
return result;
}
static class Pair {
TreeNode node;
int index;
public Pair(TreeNode node, int index) {
this.node = node;
this.index = index;
}
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(3);
root.right = new TreeNode(2);
root.left.left = new TreeNode(5);
root.right.right = new TreeNode(9);
root.left.left.left = new TreeNode(6);
root.right.right.right = new TreeNode(7);
System.out.println(widthOfBinaryTree(root));
}
}