Skip to content

Commit 2214f62

Browse files
add 222
1 parent 2e25494 commit 2214f62

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ Your ideas/fixes/algorithms are more than welcome!
341341
|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
342342
|224|[Basic Calculator](https://leetcode.com/problems/basic-calculator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_224.java)| ?|? | Hard|
343343
|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
345345
|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
346346
|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
347347
|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

src/main/java/com/fishercoder/solutions/CountCompleteTreeNodes.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy