-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountCompleteTreeNodes.java
73 lines (66 loc) · 2.27 KB
/
CountCompleteTreeNodes.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
package leetcode;
import utils.TreeNode;
import utils.Trees;
/**
* CountCompleteTreeNodes
* https://leetcode-cn.com/problems/count-complete-tree-nodes/
*
* @since 2020-11-24
*/
public class CountCompleteTreeNodes {
public static void main(String[] args) {
CountCompleteTreeNodes sol = new CountCompleteTreeNodes();
System.out.println(sol.countNodes(Trees.fromIntegers(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })));
}
public int countNodes(TreeNode root) {
Container container = new Container();
fetchMaxLevel(root, 1, -1, container);
int counts = 1;
for (int i = 1; i < container.totalLevel; i++) {
counts = counts << 1;
}
counts -= 1;
counts += container.lastLevelCounts;
return counts;
}
public int fetchMaxLevel(TreeNode subRoot, int level, int maxLevel, Container container) {
if (subRoot == null) {
return level - 1;
}
if (level == maxLevel) {
// current is max level
//System.out.println(subRoot.val);
container.lastLevelCounts++;
return maxLevel;
}
if (container.isStop) {
return maxLevel;
}
int estimateMaxLevel = Math.max(level, maxLevel);
if (subRoot.left != null) {
estimateMaxLevel = fetchMaxLevel(subRoot.left, level + 1, estimateMaxLevel, container);
if (subRoot.right != null) {
fetchMaxLevel(subRoot.right, level + 1, estimateMaxLevel, container); // ignore right sub-tree level,
// the max must in left
} else {
container.isStop = true;
}
} else {
if (level < maxLevel) {
container.isStop = true;
}
}
if (level == estimateMaxLevel) {
// keep the first max level
//System.out.println(subRoot.val);
container.totalLevel = estimateMaxLevel;
container.lastLevelCounts++;
}
return estimateMaxLevel;
}
class Container {
public int totalLevel = 0;
public int lastLevelCounts = 0;
public boolean isStop = false;
}
}