Skip to content

Commit 086c7c9

Browse files
committed
Merge pull request #168 from 3inchtime/958
add solution - 958
2 parents 2e628eb + e6bce61 commit 086c7c9

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/halfrost/LeetCode-Go/structures"
5+
)
6+
7+
// TreeNode define
8+
type TreeNode = structures.TreeNode
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
19+
func isCompleteTree(root *TreeNode) bool {
20+
queue := []*TreeNode{root}
21+
found := false
22+
23+
for len(queue) > 0 {
24+
node := queue[0] //取出每一层的第一个节点
25+
queue = queue[1:]
26+
if node == nil {
27+
found = true
28+
} else {
29+
if found {
30+
return false // 层序遍历中,两个不为空的节点中出现一个 nil
31+
}
32+
//如果左孩子为nil,则append进去的node.Left为nil
33+
queue = append(queue, node.Left, node.Right)
34+
}
35+
}
36+
return true
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question958 struct {
11+
para958
12+
ans958
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para958 struct {
18+
one []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans958 struct {
24+
one bool
25+
}
26+
27+
func Test_Problem958(t *testing.T) {
28+
29+
qs := []question958{
30+
31+
{
32+
para958{[]int{1, 2, 3, 4, 5, 6}},
33+
ans958{true},
34+
},
35+
36+
{
37+
para958{[]int{1, 2, 3, 4, 5, structures.NULL, 7}},
38+
ans958{false},
39+
},
40+
}
41+
42+
fmt.Printf("------------------------Leetcode Problem 958------------------------\n")
43+
44+
for _, q := range qs {
45+
_, p := q.ans958, q.para958
46+
fmt.Printf("【input】:%v ", p)
47+
root := structures.Ints2TreeNode(p.one)
48+
fmt.Printf("【output】:%v \n", isCompleteTree(root))
49+
}
50+
fmt.Printf("\n\n\n")
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [958. Check Completeness of a Binary Tree](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)
2+
3+
## 题目
4+
5+
Given the root of a binary tree, determine if it is a complete binary tree.
6+
7+
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.
8+
9+
Example 1:
10+
```c
11+
Input: root = [1,2,3,4,5,6]
12+
Output: true
13+
Explanation: 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.
14+
15+
1
16+
/ \
17+
2 3
18+
/ \ /
19+
4 5 6
20+
```
21+
Example 2:
22+
```c
23+
Input: root = [1,2,3,4,5,null,7]
24+
Output: false
25+
Explanation: The node with value 7 isn't as far left as possible.
26+
27+
1
28+
/ \
29+
2 3
30+
/ \ \
31+
4 5 7
32+
```
33+
34+
Constraints:
35+
36+
The number of nodes in the tree is in the range [1, 100].
37+
1 <= Node.val <= 1000
38+
39+
40+
## 题目大意
41+
42+
若设二叉树的深度为 h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。(注:第 h 层可能包含 1~ 2h 个节点。)
43+
44+
说明要判断每个节点的左孩子必须不为空。
45+
46+
47+
## 解题思路
48+
49+
- 这一题是按层序遍历的变种题。
50+
- 判断每个节点的左孩子是否为空。
51+
- 第 102,107,199 都是按层序遍历的。
52+
53+

0 commit comments

Comments
 (0)