forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountCompleteTreeNodes.java
40 lines (33 loc) · 1.08 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
public class CountCompleteTreeNodes {
/** 适合通用的二叉树,但是对于完全二叉树会超时
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int count = 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
count++;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
return count;
}*/
// 119ms,最差复杂度是O(n),最好是O(lgn)
public int countNodes(TreeNode root) {
int left = 0, right = 0;
for (TreeNode node = root; node != null; node = node.left, left++);
for (TreeNode node = root; node != null; node = node.right, right++);
if (left == right) {
return (1 << left) - 1;
} else {
return 1 + countNodes(root.left) + countNodes(root.right);
}
}
}