Skip to content

Commit 254a642

Browse files
[LEET-0000] clean up
1 parent 7919a1e commit 254a642

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

leetcode-algorithms/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
|225|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ImplementStackUsingQueues.java)| O(n)|O(n) | Easy| Stack, Queue
140140
|224|[Basic Calculator](https://leetcode.com/problems/basic-calculator/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BasicCalculator.java)| ?|? | Hard|
141141
|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RectangleArea.java)| O(1)|O(1) | Easy|
142+
|222|[Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CountCompleteTreeNodes.java)| O(?)|O(h) | Medium|
142143
|220|[Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ContainsDuplicateIII.java)| O(nlogn)|O(n) | Medium| TreeSet
143144
|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ContainsDuplicateII.java)| O(n)|O(n) | Easy| HashMap
144145
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ContainsDuplicate.java)| O(n)|O(n) | Easy| HashSet
@@ -176,7 +177,8 @@
176177
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ReverseWordsinaString.java)| O(n)|O(n) | Medium|
177178
|150|[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/EvaluateReversePolishNotation.java)| O(?)|O(?) | Medium
178179
|149|[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MaxPointsonaLine.java)| O(?)|O(?) | Hard|
179-
|147|[Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/InsertionSortList.java)| O(n^2)|O(1) | Medium| Linked List
180+
|147|[Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)|[Doubly Linked List](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/InsertionSortList.java)|[Linked Hash Map](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LRUCache_use_LinkedHashMap.java) O(n^2)|O(1) | Hard| Linked List
181+
|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LRUCache_use_DoublyLinkedList_plus_HashMap.java)| O(?)|O(?) | Hard| Linked List
180182
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreePostOrderTraversal.java)| O(n)|O(h) | Hard| Binary Tree
181183
|144|[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreePreorderTraversal.java)| O(n)|O(h) | Medium| Binary Tree
182184
|143|[Reorder List](https://leetcode.com/problems/reorder-list/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ReorderList.java)| O(n)|O(1) | Medium|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.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, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
10+
*/
11+
public class CountCompleteTreeNodes {
12+
13+
int height(TreeNode root) {
14+
return root == null ? -1 : 1 + height(root.left);
15+
}
16+
17+
public int countNodes(TreeNode root) {
18+
int nodes = 0, h = height(root);
19+
while (root != null) {
20+
if (height(root.right) == h - 1) {
21+
nodes += 1 << h;
22+
root = root.right;
23+
} else {
24+
nodes += 1 << h - 1;
25+
root = root.left;
26+
}
27+
h--;
28+
}
29+
return nodes;
30+
}
31+
32+
}

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