Skip to content

Commit d3c3f41

Browse files
committed
2022-06-02 update: added "Count Complete Tree Nodes"
1 parent 8aaace1 commit d3c3f41

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import com.smlnskgmail.jaman.leetcodejava.support.TreeNode;
4+
5+
// https://leetcode.com/problems/count-complete-tree-nodes/
6+
public class CountCompleteTreeNodes {
7+
8+
private final TreeNode input;
9+
10+
public CountCompleteTreeNodes(TreeNode input) {
11+
this.input = input;
12+
}
13+
14+
public int solution() {
15+
return countNodes(input);
16+
}
17+
18+
public int countNodes(TreeNode root) {
19+
if (root == null) {
20+
return 0;
21+
}
22+
return countNodes(root.left) + countNodes(root.right) + 1;
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import com.smlnskgmail.jaman.leetcodejava.support.TreeNode;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class CountCompleteTreeNodesTest {
9+
10+
@Test
11+
public void defaultTest() {
12+
assertEquals(
13+
6,
14+
new CountCompleteTreeNodes(
15+
new TreeNode(
16+
1,
17+
new TreeNode(
18+
2,
19+
new TreeNode(4),
20+
new TreeNode(5)
21+
),
22+
new TreeNode(
23+
3,
24+
new TreeNode(6),
25+
null
26+
)
27+
)
28+
).solution()
29+
);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)