File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed
main/java/com/smlnskgmail/jaman/leetcodejava/medium
test/java/com/smlnskgmail/jaman/leetcodejava/medium Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments