-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day40.java
63 lines (51 loc) · 1.52 KB
/
Day40.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
/* Count Complete Tree Nodes */
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
if (leftHeight == rightHeight) {
// The left subtree is a full binary tree
return (1 << leftHeight) + countNodes(root.right);
} else {
// The right subtree is a full binary tree
return (1 << rightHeight) + countNodes(root.left);
}
}
private int getHeight(TreeNode node) {
if (node == null) {
return 0;
}
return 1 + getHeight(node.left);
}
}
public class Day40 {
public static void main(String[] args) {
// Create a complete binary tree
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
Solution solution = new Solution();
int nodeCount = solution.countNodes(root);
System.out.println("Number of nodes in the binary tree: " + nodeCount);
}
}