-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxWidthBinaryTree.java
75 lines (60 loc) · 1.89 KB
/
MaxWidthBinaryTree.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
66
67
68
69
70
71
72
73
74
75
package org.sean.tree;
import java.util.LinkedList;
import java.util.Queue;
import static org.sean.utils.MathLib.log2;
/***
* 662. Maximum Width of Binary Tree
*/
public class MaxWidthBinaryTree {
Queue<TreeNode> queue = new LinkedList<>();
private int max = 1;
public int widthOfBinaryTree(TreeNode root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 1;
LinkedList<Integer> resultList = new LinkedList<>();
LinkedList<Integer> sequences = new LinkedList<>();
sequences.add(1);
int count = 0;
queue.add(root);
int preDepth = -1;
while (!queue.isEmpty()) {
TreeNode node = queue.remove();
count = sequences.remove();
if (node != null) {
int depth = log2(count);
if (preDepth != depth) {
updateMaxWidth(resultList);
resultList.clear();
resultList.add(count);
preDepth = depth;
} else {
resultList.add(count);
}
if (node.left != null) {
sequences.add(count * 2);
queue.add(node.left);
}
if (node.right != null) {
sequences.add(count * 2 + 1);
queue.add(node.right);
}
}
}
// For the last level
if (!resultList.isEmpty()) {
updateMaxWidth(resultList);
}
return max;
}
private void updateMaxWidth(LinkedList<Integer> resultList) {
int size = resultList.size();
if (size > 1) {
int distance = resultList.get(size - 1) - resultList.get(0) + 1;
if (distance > max) {
max = distance;
}
}
}
}