Skip to content

Commit 68f4184

Browse files
committed
+ problem 222
1 parent d7f3f61 commit 68f4184

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 222. Count Complete Tree Nodes
2+
Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
3+
4+
According to [**Wikipedia**](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees), every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between `1` and <code>2<sup>h</sup></code> nodes inclusive at the last level `h`.
5+
6+
Design an algorithm that runs in less than `O(n)` time complexity.
7+
8+
#### Example 1:
9+
![](https://assets.leetcode.com/uploads/2021/01/14/complete.jpg)
10+
<pre>
11+
<strong>Input:</strong> root = [1,2,3,4,5,6]
12+
<strong>Output:</strong> 6
13+
</pre>
14+
15+
#### Example 2:
16+
<pre>
17+
<strong>Input:</strong> root = []
18+
<strong>Output:</strong> 0
19+
</pre>
20+
21+
#### Example 3:
22+
<pre>
23+
<strong>Input:</strong> root = [1]
24+
<strong>Output:</strong> 1
25+
</pre>
26+
27+
#### Constraints:
28+
* The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
29+
* <code>0 <= Node.val <= 5 * 10<sup>4</sup></code>
30+
* The tree is guaranteed to be **complete**.
31+
32+
## Solutions (Python)
33+
34+
### 1. Solution
35+
```Python
36+
# Definition for a binary tree node.
37+
# class TreeNode:
38+
# def __init__(self, val=0, left=None, right=None):
39+
# self.val = val
40+
# self.left = left
41+
# self.right = right
42+
class Solution:
43+
def countNodes(self, root: Optional[TreeNode]) -> int:
44+
low = 0
45+
high = 1
46+
curr = root
47+
root = TreeNode(left=TreeNode(), right=root)
48+
49+
while curr is not None:
50+
low = (low << 1) + 1
51+
high = (high << 1) + 1
52+
curr = curr.right
53+
54+
while low < high:
55+
mid = (low + high) // 2
56+
curr = root
57+
flag = True
58+
59+
for bit in bin(mid)[2:]:
60+
if bit == '0':
61+
curr = curr.left
62+
else:
63+
curr = curr.right
64+
65+
if curr is None:
66+
flag = False
67+
break
68+
69+
if flag:
70+
low = mid + 1
71+
else:
72+
high = mid
73+
74+
return low - 1
75+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 222. 完全二叉树的节点个数
2+
给你一棵 **完全二叉树** 的根节点 `root` ,求出该树的节点个数。
3+
4+
[完全二叉树](https://baike.baidu.com/item/%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91/7773232?fr=aladdin) 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 `h` 层,则该层包含 <code>1~ 2<sup>h</sup></code> 个节点。
5+
6+
#### 示例 1:
7+
![](https://assets.leetcode.com/uploads/2021/01/14/complete.jpg)
8+
<pre>
9+
<strong>输入:</strong> root = [1,2,3,4,5,6]
10+
<strong>输出:</strong> 6
11+
</pre>
12+
13+
#### 示例 2:
14+
<pre>
15+
<strong>输入:</strong> root = []
16+
<strong>输出:</strong> 0
17+
</pre>
18+
19+
#### 示例 3:
20+
<pre>
21+
<strong>输入:</strong> root = [1]
22+
<strong>输出:</strong> 1
23+
</pre>
24+
25+
#### 提示:
26+
* 树中节点的数目范围是<code>[0, 5 * 10<sup>4</sup>]</code>
27+
* <code>0 <= Node.val <= 5 * 10<sup>4</sup></code>
28+
* 题目数据保证输入的树是 **完全二叉树**
29+
30+
**进阶:**遍历树来统计节点是一种时间复杂度为 `O(n)` 的简单解决方案。你可以设计一个更快的算法吗?
31+
32+
## 题解 (Python)
33+
34+
### 1. 题解
35+
```Python
36+
# Definition for a binary tree node.
37+
# class TreeNode:
38+
# def __init__(self, val=0, left=None, right=None):
39+
# self.val = val
40+
# self.left = left
41+
# self.right = right
42+
class Solution:
43+
def countNodes(self, root: Optional[TreeNode]) -> int:
44+
low = 0
45+
high = 1
46+
curr = root
47+
root = TreeNode(left=TreeNode(), right=root)
48+
49+
while curr is not None:
50+
low = (low << 1) + 1
51+
high = (high << 1) + 1
52+
curr = curr.right
53+
54+
while low < high:
55+
mid = (low + high) // 2
56+
curr = root
57+
flag = True
58+
59+
for bit in bin(mid)[2:]:
60+
if bit == '0':
61+
curr = curr.left
62+
else:
63+
curr = curr.right
64+
65+
if curr is None:
66+
flag = False
67+
break
68+
69+
if flag:
70+
low = mid + 1
71+
else:
72+
high = mid
73+
74+
return low - 1
75+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def countNodes(self, root: Optional[TreeNode]) -> int:
9+
low = 0
10+
high = 1
11+
curr = root
12+
root = TreeNode(left=TreeNode(), right=root)
13+
14+
while curr is not None:
15+
low = (low << 1) + 1
16+
high = (high << 1) + 1
17+
curr = curr.right
18+
19+
while low < high:
20+
mid = (low + high) // 2
21+
curr = root
22+
flag = True
23+
24+
for bit in bin(mid)[2:]:
25+
if bit == '0':
26+
curr = curr.left
27+
else:
28+
curr = curr.right
29+
30+
if curr is None:
31+
flag = False
32+
break
33+
34+
if flag:
35+
low = mid + 1
36+
else:
37+
high = mid
38+
39+
return low - 1

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
[219][219l] |[Contains Duplicate II][219] |![rs]
147147
[220][220l] |[Contains Duplicate III][220] |![rs]
148148
[221][221l] |[Maximal Square][221] |![rs]
149+
[222][222l] |[Count Complete Tree Nodes][222] |![py]
149150
[223][223l] |[Rectangle Area][223] |![rb]&nbsp;&nbsp;![rs]
150151
[225][225l] |[Implement Stack using Queues][225] |![rs]
151152
[226][226l] |[Invert Binary Tree][226] |![py]
@@ -1388,6 +1389,7 @@
13881389
[219]:Problemset/0219-Contains%20Duplicate%20II/README.md#219-contains-duplicate-ii
13891390
[220]:Problemset/0220-Contains%20Duplicate%20III/README.md#220-contains-duplicate-iii
13901391
[221]:Problemset/0221-Maximal%20Square/README.md#221-maximal-square
1392+
[222]:Problemset/0222-Count%20Complete%20Tree%20Nodes/README.md#222-count-complete-tree-nodes
13911393
[223]:Problemset/0223-Rectangle%20Area/README.md#223-rectangle-area
13921394
[225]:Problemset/0225-Implement%20Stack%20using%20Queues/README.md#225-implement-stack-using-queues
13931395
[226]:Problemset/0226-Invert%20Binary%20Tree/README.md#226-invert-binary-tree
@@ -2627,6 +2629,7 @@
26272629
[219l]:https://leetcode.com/problems/contains-duplicate-ii/
26282630
[220l]:https://leetcode.com/problems/contains-duplicate-iii/
26292631
[221l]:https://leetcode.com/problems/maximal-square/
2632+
[222l]:https://leetcode.com/problems/count-complete-tree-nodes/
26302633
[223l]:https://leetcode.com/problems/rectangle-area/
26312634
[225l]:https://leetcode.com/problems/implement-stack-using-queues/
26322635
[226l]:https://leetcode.com/problems/invert-binary-tree/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
[219][219l] |[存在重复元素 II][219] |![rs]
147147
[220][220l] |[存在重复元素 III][220] |![rs]
148148
[221][221l] |[最大正方形][221] |![rs]
149+
[222][222l] |[完全二叉树的节点个数][222] |![py]
149150
[223][223l] |[矩形面积][223] |![rb]&nbsp;&nbsp;![rs]
150151
[225][225l] |[用队列实现栈][225] |![rs]
151152
[226][226l] |[翻转二叉树][226] |![py]
@@ -1388,6 +1389,7 @@
13881389
[219]:Problemset/0219-Contains%20Duplicate%20II/README_CN.md#219-存在重复元素-ii
13891390
[220]:Problemset/0220-Contains%20Duplicate%20III/README_CN.md#220-存在重复元素-iii
13901391
[221]:Problemset/0221-Maximal%20Square/README_CN.md#221-最大正方形
1392+
[222]:Problemset/0222-Count%20Complete%20Tree%20Nodes/README_CN.md#222-完全二叉树的节点个数
13911393
[223]:Problemset/0223-Rectangle%20Area/README_CN.md#223-矩形面积
13921394
[225]:Problemset/0225-Implement%20Stack%20using%20Queues/README_CN.md#225-用队列实现栈
13931395
[226]:Problemset/0226-Invert%20Binary%20Tree/README_CN.md#226-翻转二叉树
@@ -2627,6 +2629,7 @@
26272629
[219l]:https://leetcode.cn/problems/contains-duplicate-ii/
26282630
[220l]:https://leetcode.cn/problems/contains-duplicate-iii/
26292631
[221l]:https://leetcode.cn/problems/maximal-square/
2632+
[222l]:https://leetcode.cn/problems/count-complete-tree-nodes/
26302633
[223l]:https://leetcode.cn/problems/rectangle-area/
26312634
[225l]:https://leetcode.cn/problems/implement-stack-using-queues/
26322635
[226l]:https://leetcode.cn/problems/invert-binary-tree/

0 commit comments

Comments
 (0)