File tree Expand file tree Collapse file tree 4 files changed +39
-10
lines changed
Expand file tree Collapse file tree 4 files changed +39
-10
lines changed Original file line number Diff line number Diff line change 178178| 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 ) | |
179179| 220 | [ Contains Duplicate III] ( https://leetcode.com/problems/contains-duplicate-iii ) | | |
180180| 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 ) | |
182182| 223 | [ Rectangle Area] ( https://leetcode.com/problems/rectangle-area ) | | |
183183| 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 ) | |
184184| 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 {
22 int val ;
33 TreeNode left ;
44 TreeNode right ;
@@ -9,4 +9,19 @@ public class TreeNode {
99 this .left = left ;
1010 this .right = right ;
1111 }
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+ }
1227}
Original file line number Diff line number Diff line change 77 *
88 * @author saiteja
99 */
10- public class TreePrinter
11- {
10+ public class TreePrinter {
1211 /** Node that can be printed */
13- public interface PrintableNode
14- {
12+ public interface PrintableNode {
1513 /** Get left child */
1614 PrintableNode getLeft ();
1715
18-
1916 /** Get right child */
2017 PrintableNode getRight ();
2118
22-
2319 /** Get text to be printed */
2420 String getText ();
2521 }
@@ -31,8 +27,7 @@ public interface PrintableNode
3127 * @param root
3228 * tree root node
3329 */
34- public static void print (PrintableNode root )
35- {
30+ public static void print (PrintableNode root ) {
3631 List <List <String >> lines = new ArrayList <List <String >>();
3732
3833 List <PrintableNode > level = new ArrayList <PrintableNode >();
You can’t perform that action at this time.
0 commit comments