-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountCompleteTreeNodes.java
More file actions
66 lines (53 loc) · 1.86 KB
/
CountCompleteTreeNodes.java
File metadata and controls
66 lines (53 loc) · 1.86 KB
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
public class CountCompleteTreeNodes {
/**
*
Given a complete binary tree, count the number of nodes.
Note:
Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Example:
Input:
1
/ \
2 3
/ \ /
4 5 6
Output: 6
*/
public int countNodes(TreeNode root) {
if (root == null) return 0;
int level = 0;
TreeNode cur = root;
while (cur != null) {
level++;
cur = cur.left;
}
int totalNodesBeforeLastLevel = (1 << (level - 1)) - 1;
return totalNodesBeforeLastLevel + nodesOfLastLevel(root, level);
}
public int nodesOfLastLevel(TreeNode root, int level) {
if (root == null) return 0;
if (level == 1 && root != null) return 1;
if (level == 2) {
if (root.right != null) return 2;
if (root.left != null) return 1;
return 0;
}
TreeNode midNode = root.left;
for (int i = 0; i < level - 2; i++) {
midNode = midNode.right;
}
if (midNode == null) return nodesOfLastLevel(root.left, level - 1);
return (1 << (level - 2)) + nodesOfLastLevel(root.right, level - 1);
}
public static void main(String[] args) {
CountCompleteTreeNodes a = new CountCompleteTreeNodes();
TreeNode b = new TreeNode(1);
b.left = new TreeNode(2);
b.right = new TreeNode(3);
b.left.left = new TreeNode(4);
b.left.right = new TreeNode(5);
b.right.left = new TreeNode(6);
System.out.println(a.countNodes(b));
}
}