File tree 3 files changed +50
-33
lines changed
src/main/java/com/fishercoder/solutions 3 files changed +50
-33
lines changed Original file line number Diff line number Diff line change @@ -341,7 +341,7 @@ Your ideas/fixes/algorithms are more than welcome!
341
341
|225|[ Implement Stack using Queues] ( https://leetcode.com/problems/implement-stack-using-queues/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/ImplementStackUsingQueues.java ) | O(n)|O(n) | Easy| Stack, Queue
342
342
| 224| [ Basic Calculator] ( https://leetcode.com/problems/basic-calculator/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_224.java ) | ?| ? | Hard|
343
343
| 223| [ Rectangle Area] ( https://leetcode.com/problems/rectangle-area/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/RectangleArea.java ) | O(1)| O(1) | Easy|
344
- | 222| [ Count Complete Tree Nodes] ( https://leetcode.com/problems/count-complete-tree-nodes/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/CountCompleteTreeNodes .java ) | O(?)| O(h) | Medium|
344
+ |222|[ Count Complete Tree Nodes] ( https://leetcode.com/problems/count-complete-tree-nodes/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_222 .java ) | O(?)|O(h) | Medium| Recursion
345
345
|220|[ Contains Duplicate III] ( https://leetcode.com/problems/contains-duplicate-iii/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/ContainsDuplicateIII.java ) | O(nlogn)|O(n) | Medium| TreeSet
346
346
|219|[ Contains Duplicate II] ( https://leetcode.com/problems/contains-duplicate-ii/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/ContainsDuplicateII.java ) | O(n)|O(n) | Easy| HashMap
347
347
|218|[ The Skyline Problem] ( https://leetcode.com/problems/the-skyline-problem/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_218.java ) | O(n)|O(n) | Hard| TreeMap, Design
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package com .fishercoder .solutions ;
2
+
3
+ import com .fishercoder .common .classes .TreeNode ;
4
+
5
+ /**
6
+ * Given a complete binary tree, count the number of nodes.
7
+ * <p>
8
+ * Definition of a complete binary tree from Wikipedia:
9
+ * In a complete binary tree every level,
10
+ * except possibly the last, is completely filled,
11
+ * and all nodes in the last level are as far left as possible.
12
+ * It can have between 1 and 2h nodes inclusive at the last level h.
13
+ */
14
+ public class _222 {
15
+
16
+ class Solution1 {
17
+ /**reference: https://discuss.leetcode.com/topic/21317/accepted-easy-understand-java-solution/2*/
18
+ public int countNodes (TreeNode root ) {
19
+ int leftH = getLeftHeight (root );
20
+ int rightH = getRightHeight (root );
21
+ if (leftH == rightH ) return (1 << leftH ) - 1 ;
22
+ else return 1 + countNodes (root .left ) + countNodes (root .right );
23
+ }
24
+
25
+ private int getRightHeight (TreeNode root ) {
26
+ int height = 0 ;
27
+ while (root != null ) {
28
+ root = root .right ;
29
+ height ++;
30
+ }
31
+ return height ;
32
+ }
33
+
34
+ private int getLeftHeight (TreeNode root ) {
35
+ int height = 0 ;
36
+ while (root != null ) {
37
+ root = root .left ;
38
+ height ++;
39
+ }
40
+ return height ;
41
+ }
42
+
43
+ }
44
+
45
+ public static void main (String ...args ) {
46
+ System .out .println (1 << 3 );
47
+ }
48
+
49
+ }
You can’t perform that action at this time.
0 commit comments