Repository navigation
Expand file tree
/
Copy pathWidestBinaryTreeLevel.java
More file actions
60 lines (56 loc) · 1.85 KB
/
Copy pathWidestBinaryTreeLevel.java
File metadata and controls
60 lines (56 loc) · 1.85 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
import java.util.ArrayDeque;
import java.util.Queue;
import DS.TreeNode;
/*
// Definition of TreeNode:
class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class WidestBinaryTreeLevel {
public class Pair {
TreeNode node;
int index;
public Pair(TreeNode node, int index) {
this.node = node;
this.index = index;
}
}
public int widestBinaryTreeLevel(TreeNode root) {
if (root == null) {
return 0;
}
int maxWidth = 0;
Queue<Pair> queue = new ArrayDeque<>();
queue.offer(new Pair(root, 0)); // Stores (node, index) pairs.
while (!queue.isEmpty()) {
int levelSize = queue.size();
// Set the 'leftmostIndex' to the index of the first node in
// this level. Start 'rightmostIndex' at the same point as
// 'leftmostIndex' and update it as we traverse the level,
// eventually positioning it at the last node.
int leftmostIndex = queue.peek().index;
int rightmostIndex = leftmostIndex;
// Process all nodes at the current level.
for (int j = 0; j < levelSize; j++) {
Pair pair = queue.poll();
TreeNode node = pair.node;
int i = pair.index;
if (node.left != null) {
queue.offer(new Pair(node.left, 2 * i + 1));
}
if (node.right != null) {
queue.offer(new Pair(node.right, 2 * i + 2));
}
rightmostIndex = i;
}
maxWidth = Math.max(maxWidth, rightmostIndex - leftmostIndex + 1);
}
return maxWidth;
}
}