File tree 4 files changed +39
-10
lines changed
4 files changed +39
-10
lines changed Original file line number Diff line number Diff line change 178
178
| 219 | [ Contains Duplicate II] ( https://leetcode.com/problems/contains-duplicate-ii ) | [ ![ Java] ( assets/java.png )] ( src/ContainsDuplicateII.java ) [ ![ Python] ( assets/python.png )] ( python/contains_duplicate_ii.py ) | |
179
179
| 220 | [ Contains Duplicate III] ( https://leetcode.com/problems/contains-duplicate-iii ) | | |
180
180
| 221 | [ Maximal Square] ( https://leetcode.com/problems/maximal-square ) | | |
181
- | 222 | [ Count Complete Tree Nodes] ( https://leetcode.com/problems/count-complete-tree-nodes ) | | |
181
+ | 222 | [ Count Complete Tree Nodes] ( https://leetcode.com/problems/count-complete-tree-nodes ) | [ ![ Java ] ( assets/java.png )] ( src/CountCompleteTreeNodes.java ) | |
182
182
| 223 | [ Rectangle Area] ( https://leetcode.com/problems/rectangle-area ) | | |
183
183
| 225 | [ Implement Stack using Queues] ( https://leetcode.com/problems/implement-stack-using-queues ) | [ ![ Java] ( assets/java.png )] ( src/MyStack.java ) [ ![ Python] ( assets/python.png )] ( python/implement_stack_using_queues.py ) | |
184
184
| 226 | [ Invert Binary Tree] ( https://leetcode.com/problems/invert-binary-tree ) | [ ![ Java] ( assets/java.png )] ( src/InvertBinaryTree.java ) [ ![ Python] ( assets/python.png )] ( python/invert_binary_tree.py ) | |
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/count-complete-tree-nodes
2
+ // T: O(log(n) ^ 2)
3
+ // S: O(log(n))
4
+
5
+ public class CountCompleteTreeNodes {
6
+ private static int depth (TreeNode root ) {
7
+ if (root == null ) return 0 ;
8
+ return 1 + depth (root .left );
9
+ }
10
+
11
+ public static int countNodes (TreeNode root ) {
12
+ if (root == null ) return 0 ;
13
+ int leftDepth = depth (root .left );
14
+ int rightDepth = depth (root .right );
15
+ return 1 + (leftDepth == rightDepth
16
+ ? ((1 << leftDepth ) - 1 ) + countNodes (root .right )
17
+ : countNodes (root .left ) + countNodes (root .right ));
18
+ }
19
+ }
Original file line number Diff line number Diff line change 1
- public class TreeNode {
1
+ public class TreeNode implements TreePrinter . PrintableNode {
2
2
int val ;
3
3
TreeNode left ;
4
4
TreeNode right ;
@@ -9,4 +9,19 @@ public class TreeNode {
9
9
this .left = left ;
10
10
this .right = right ;
11
11
}
12
+
13
+ @ Override
14
+ public TreePrinter .PrintableNode getLeft () {
15
+ return this .left ;
16
+ }
17
+
18
+ @ Override
19
+ public TreePrinter .PrintableNode getRight () {
20
+ return this .right ;
21
+ }
22
+
23
+ @ Override
24
+ public String getText () {
25
+ return this .val + "" ;
26
+ }
12
27
}
Original file line number Diff line number Diff line change 7
7
*
8
8
* @author saiteja
9
9
*/
10
- public class TreePrinter
11
- {
10
+ public class TreePrinter {
12
11
/** Node that can be printed */
13
- public interface PrintableNode
14
- {
12
+ public interface PrintableNode {
15
13
/** Get left child */
16
14
PrintableNode getLeft ();
17
15
18
-
19
16
/** Get right child */
20
17
PrintableNode getRight ();
21
18
22
-
23
19
/** Get text to be printed */
24
20
String getText ();
25
21
}
@@ -31,8 +27,7 @@ public interface PrintableNode
31
27
* @param root
32
28
* tree root node
33
29
*/
34
- public static void print (PrintableNode root )
35
- {
30
+ public static void print (PrintableNode root ) {
36
31
List <List <String >> lines = new ArrayList <List <String >>();
37
32
38
33
List <PrintableNode > level = new ArrayList <PrintableNode >();
You can’t perform that action at this time.
0 commit comments