Skip to content

Commit aa8633e

Browse files
committed
Add solution #222
1 parent c826760 commit aa8633e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
217|[Contains Duplicate](./0217-contains-duplicate.js)|Easy|
197197
219|[Contains Duplicate II](./0219-contains-duplicate-ii.js)|Easy|
198198
220|[Contains Duplicate III](./0220-contains-duplicate-iii.js)|Hard|
199+
222|[Count Complete Tree Nodes](./0222-count-complete-tree-nodes.js)|Easy|
199200
225|[Implement Stack using Queues](./0225-implement-stack-using-queues.js)|Easy|
200201
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|
201202
228|[Summary Ranges](./0228-summary-ranges.js)|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 222. Count Complete Tree Nodes
3+
* https://leetcode.com/problems/count-complete-tree-nodes/
4+
* Difficulty: Easy
5+
*
6+
* Given the root of a complete binary tree, return the number of the nodes in the tree.
7+
*
8+
* According to Wikipedia, every level, except possibly the last, is completely filled
9+
* in a complete binary tree, and all nodes in the last level are as far left as possible.
10+
* It can have between 1 and 2h nodes inclusive at the last level h.
11+
*
12+
* Design an algorithm that runs in less than O(n) time complexity.
13+
*/
14+
15+
/**
16+
* Definition for a binary tree node.
17+
* function TreeNode(val, left, right) {
18+
* this.val = (val===undefined ? 0 : val)
19+
* this.left = (left===undefined ? null : left)
20+
* this.right = (right===undefined ? null : right)
21+
* }
22+
*/
23+
/**
24+
* @param {TreeNode} root
25+
* @return {number}
26+
*/
27+
var countNodes = function(root) {
28+
return !root ? 0 : 1 + countNodes(root.left) + countNodes(root.right);
29+
};

0 commit comments

Comments
 (0)