-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaximumWidthOfBinaryTree.java
61 lines (52 loc) · 1.81 KB
/
MaximumWidthOfBinaryTree.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
package com.smlnskgmail.jaman.leetcodejava.medium;
import com.smlnskgmail.jaman.leetcodejava.support.TreeNode;
import java.util.Deque;
import java.util.LinkedList;
// https://leetcode.com/problems/maximum-width-of-binary-tree/
public class MaximumWidthOfBinaryTree {
private final TreeNode input;
public MaximumWidthOfBinaryTree(TreeNode input) {
this.input = input;
}
public int solution() {
int result = 1;
Deque<TreeNodePair> nodes = new LinkedList<>();
nodes.add(new TreeNodePair(input, 0));
while (!nodes.isEmpty()) {
int size = nodes.size();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNodePair pair = nodes.removeFirst();
min = Math.min(min, pair.value);
max = Math.max(max, pair.value);
if (pair.node.left != null) {
nodes.addLast(
new TreeNodePair(
pair.node.left,
pair.value * 2 + 1
)
);
}
if (pair.node.right != null) {
nodes.addLast(
new TreeNodePair(
pair.node.right,
pair.value * 2 + 2
)
);
}
}
result = Math.max(result, max - min + 1);
}
return result;
}
private static class TreeNodePair {
final TreeNode node;
final int value;
private TreeNodePair(TreeNode node, int value) {
this.node = node;
this.value = value;
}
}
}