forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
54 lines (49 loc) · 1.06 KB
/
Solution.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
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
int h = 0;
TreeNode curr = root;
while (curr.left != null) {
curr = curr.left;
h++;
}
if (h == 0) return 1;
int low = 1 << h, high = 1 << (h + 1);
while (low < high) {
int mid = low + (high - low) / 2;
if (valid(root, mid, h)) {
low = mid + 1;
} else {
high = mid;
}
}
return low - 1;
}
private boolean valid(TreeNode root, int pos, int h) {
TreeNode curr = root;
for (int i = h - 1; i >= 0; i--) {
if ((pos & (1 << i)) > 0) {
curr = curr.right;
} else {
curr = curr.left;
}
}
return curr != null;
}
public static void main(String[] args) {
Solution s = new Solution();
TreeNode root = new TreeNode(0);
System.out.println(s.countNodes(root));
root.left = new TreeNode(1);
System.out.println(s.countNodes(root));
root.right = new TreeNode(2);
System.out.println(s.countNodes(root));
}
}