Skip to content

Commit 98ba3ed

Browse files
committed
+ problem 958
1 parent b10e32e commit 98ba3ed

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 958. Check Completeness of a Binary Tree
2+
Given the `root` of a binary tree, determine if it is a *complete binary tree*.
3+
4+
In a [**complete binary**](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees) 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 <code>2<sup>h</sup></code> nodes inclusive at the last level `h`.
5+
6+
#### Example 1:
7+
![](https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-1.png)
8+
<pre>
9+
<strong>Input:</strong> root = [1,2,3,4,5,6]
10+
<strong>Output:</strong> true
11+
<strong>Explanation:</strong> Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.
12+
</pre>
13+
14+
#### Example 2:
15+
![](https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-2.png)
16+
<pre>
17+
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
18+
<strong>Output:</strong> false
19+
<strong>Explanation:</strong> The node with value 7 isn't as far left as possible.
20+
</pre>
21+
22+
#### Constraints:
23+
* The number of nodes in the tree is in the range `[1, 100]`.
24+
* `1 <= Node.val <= 1000`
25+
26+
## Solutions (Python)
27+
28+
### 1. Solution
29+
```Python
30+
# Definition for a binary tree node.
31+
# class TreeNode:
32+
# def __init__(self, val=0, left=None, right=None):
33+
# self.val = val
34+
# self.left = left
35+
# self.right = right
36+
class Solution:
37+
def isCompleteTree(self, root: Optional[TreeNode]) -> bool:
38+
nodes = [root]
39+
depth = 0
40+
41+
while True:
42+
children = []
43+
44+
if nodes[0] == None:
45+
return True
46+
47+
if len(nodes) != 2 ** depth:
48+
return False
49+
50+
for node in nodes:
51+
if node is not None:
52+
children.append(node.left)
53+
children.append(node.right)
54+
55+
for i in range(1, len(children)):
56+
if children[i - 1] is None and children[i] is not None:
57+
return False
58+
59+
nodes = children
60+
depth += 1
61+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 958. 二叉树的完全性检验
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` 层)中可以包含 `1` 到 <code>2<sup>h</sup></code> 个节点。
5+
6+
#### 示例 1:
7+
![](https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-1.png)
8+
<pre>
9+
<strong>输入:</strong> root = [1,2,3,4,5,6]
10+
<strong>输出:</strong> true
11+
<strong>解释:</strong> 最后一层前的每一层都是满的(即,节点值为 {1} 和 {2,3} 的两层),且最后一层中的所有节点({4,5,6})尽可能靠左。
12+
</pre>
13+
14+
#### 示例 2:
15+
![](https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-2.png)
16+
<pre>
17+
<strong>输入:</strong> root = [1,2,3,4,5,null,7]
18+
<strong>输出:</strong> false
19+
<strong>解释:</strong> 值为 7 的节点不满足条件「节点尽可能靠左」。
20+
</pre>
21+
22+
#### 提示:
23+
* 树中节点数目在范围 `[1, 100]`
24+
* `1 <= Node.val <= 1000`
25+
26+
## 题解 (Python)
27+
28+
### 1. 题解
29+
```Python
30+
# Definition for a binary tree node.
31+
# class TreeNode:
32+
# def __init__(self, val=0, left=None, right=None):
33+
# self.val = val
34+
# self.left = left
35+
# self.right = right
36+
class Solution:
37+
def isCompleteTree(self, root: Optional[TreeNode]) -> bool:
38+
nodes = [root]
39+
depth = 0
40+
41+
while True:
42+
children = []
43+
44+
if nodes[0] == None:
45+
return True
46+
47+
if len(nodes) != 2 ** depth:
48+
return False
49+
50+
for node in nodes:
51+
if node is not None:
52+
children.append(node.left)
53+
children.append(node.right)
54+
55+
for i in range(1, len(children)):
56+
if children[i - 1] is None and children[i] is not None:
57+
return False
58+
59+
nodes = children
60+
depth += 1
61+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 isCompleteTree(self, root: Optional[TreeNode]) -> bool:
9+
nodes = [root]
10+
depth = 0
11+
12+
while True:
13+
children = []
14+
15+
if nodes[0] == None:
16+
return True
17+
18+
if len(nodes) != 2 ** depth:
19+
return False
20+
21+
for node in nodes:
22+
if node is not None:
23+
children.append(node.left)
24+
children.append(node.right)
25+
26+
for i in range(1, len(children)):
27+
if children[i - 1] is None and children[i] is not None:
28+
return False
29+
30+
nodes = children
31+
depth += 1

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@
546546
[953][953l] |[Verifying an Alien Dictionary][953] |![py]
547547
[954][954l] |[Array of Doubled Pairs][954] |![py]
548548
[957][957l] |[Prison Cells After N Days][957] |![rs]
549+
[958][958l] |[Check Completeness of a Binary Tree][958] |![py]
549550
[961][961l] |[N-Repeated Element in Size 2N Array][961] |![rs]
550551
[962][962l] |[Maximum Width Ramp][962] |![rs]
551552
[965][965l] |[Univalued Binary Tree][965] |![py]
@@ -1865,6 +1866,7 @@
18651866
[953]:Problemset/0953-Verifying%20an%20Alien%20Dictionary/README.md#953-verifying-an-alien-dictionary
18661867
[954]:Problemset/0954-Array%20of%20Doubled%20Pairs/README.md#954-array-of-doubled-pairs
18671868
[957]:Problemset/0957-Prison%20Cells%20After%20N%20Days/README.md#957-prison-cells-after-n-days
1869+
[958]:Problemset/0958-Check%20Completeness%20of%20a%20Binary%20Tree/README.md#958-check-completeness-of-a-binary-tree
18681870
[961]:Problemset/0961-N-Repeated%20Element%20in%20Size%202N%20Array/README.md#961-n-repeated-element-in-size-2n-array
18691871
[962]:Problemset/0962-Maximum%20Width%20Ramp/README.md#962-maximum-width-ramp
18701872
[965]:Problemset/0965-Univalued%20Binary%20Tree/README.md#965-univalued-binary-tree
@@ -3187,6 +3189,7 @@
31873189
[953l]:https://leetcode.com/problems/verifying-an-alien-dictionary/
31883190
[954l]:https://leetcode.com/problems/array-of-doubled-pairs/
31893191
[957l]:https://leetcode.com/problems/prison-cells-after-n-days/
3192+
[958l]:https://leetcode.com/problems/check-completeness-of-a-binary-tree/
31903193
[961l]:https://leetcode.com/problems/n-repeated-element-in-size-2n-array/
31913194
[962l]:https://leetcode.com/problems/maximum-width-ramp/
31923195
[965l]:https://leetcode.com/problems/univalued-binary-tree/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@
546546
[953][953l] |[验证外星语词典][953] |![py]
547547
[954][954l] |[二倍数对数组][954] |![py]
548548
[957][957l] |[N 天后的牢房][957] |![rs]
549+
[958][958l] |[二叉树的完全性检验][958] |![py]
549550
[961][961l] |[重复 N 次的元素][961] |![rs]
550551
[962][962l] |[最大宽度坡][962] |![rs]
551552
[965][965l] |[单值二叉树][965] |![py]
@@ -1865,6 +1866,7 @@
18651866
[953]:Problemset/0953-Verifying%20an%20Alien%20Dictionary/README_CN.md#953-验证外星语词典
18661867
[954]:Problemset/0954-Array%20of%20Doubled%20Pairs/README_CN.md#954-二倍数对数组
18671868
[957]:Problemset/0957-Prison%20Cells%20After%20N%20Days/README_CN.md#957-n-天后的牢房
1869+
[958]:Problemset/0958-Check%20Completeness%20of%20a%20Binary%20Tree/README_CN.md#958-二叉树的完全性检验
18681870
[961]:Problemset/0961-N-Repeated%20Element%20in%20Size%202N%20Array/README_CN.md#961-重复-n-次的元素
18691871
[962]:Problemset/0962-Maximum%20Width%20Ramp/README_CN.md#962-最大宽度坡
18701872
[965]:Problemset/0965-Univalued%20Binary%20Tree/README_CN.md#965-单值二叉树
@@ -3187,6 +3189,7 @@
31873189
[953l]:https://leetcode.cn/problems/verifying-an-alien-dictionary/
31883190
[954l]:https://leetcode.cn/problems/array-of-doubled-pairs/
31893191
[957l]:https://leetcode.cn/problems/prison-cells-after-n-days/
3192+
[958l]:https://leetcode.cn/problems/check-completeness-of-a-binary-tree/
31903193
[961l]:https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/
31913194
[962l]:https://leetcode.cn/problems/maximum-width-ramp/
31923195
[965l]:https://leetcode.cn/problems/univalued-binary-tree/

0 commit comments

Comments
 (0)