Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d00930e

Browse files
committed
commit solution 107
1 parent 454704a commit d00930e

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

‎index-tags.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
151151
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
152152
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
153+
| [107](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii) | [二叉树的层次遍历 ii](/solution/100-199/0107.binary-tree-level-order-traversal-ii/) | ``,`广度优先搜索` | <font color=green>简单</font> ||
153154

154155
#### **递归**
155156

@@ -166,6 +167,7 @@
166167
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
167168
| --- | --- | --- | --- | --- |
168169
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
170+
| [107](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii) | [二叉树的层次遍历 ii](/solution/100-199/0107.binary-tree-level-order-traversal-ii/) | ``,`广度优先搜索` | <font color=green>简单</font> ||
169171

170172
#### **贪心算法**
171173

‎index-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
3131
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
3232
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
33-
| [107](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii) | [二叉树的层次遍历 ii](/solution/100-199/0107.binary-tree-level-order-traversal-ii/) | ``,`广度优先搜索` | <font color=green>简单</font> |
33+
| [107](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii) | [二叉树的层次遍历 ii](/solution/100-199/0107.binary-tree-level-order-traversal-ii/) | ``,`广度优先搜索` | <font color=green>简单</font> ||
3434
| [108](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree) | [将有序数组转换为二叉搜索树](/solution/100-199/0108.convert-sorted-array-to-binary-search-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
3535
| [110](https://leetcode-cn.com/problems/balanced-binary-tree) | [平衡二叉树](/solution/100-199/0110.balanced-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
3636
| [111](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree) | [二叉树的最小深度](/solution/100-199/0111.minimum-depth-of-binary-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> |
Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
1-
# [100. xxx](https://leetcode-cn.com/problems/recover-binary-search-tree)
1+
# [107. 二叉树的层次遍历 II](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/description/)
22

33
### 题目描述
44

5+
<p>给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)</p>
6+
7+
<p>例如:<br>
8+
给定二叉树 <code>[3,9,20,null,null,15,7]</code>,</p>
9+
10+
<pre> 3
11+
/ \
12+
9 20
13+
/ \
14+
15 7
15+
</pre>
16+
17+
<p>返回其自底向上的层次遍历为:</p>
18+
19+
<pre>[
20+
[15,7],
21+
[9,20],
22+
[3]
23+
]
24+
</pre>
525

626
### 解题思路
727

28+
1. BFS
829

930
### 具体解法
1031

11-
<!-- tabs:start -->
12-
1332
#### **Golang**
1433
```go
15-
34+
func levelOrderBottom(root *TreeNode) [][]int {
35+
var res [][]int
36+
if root == nil {
37+
return res
38+
}
39+
queue := []*TreeNode{root}
40+
41+
for len(queue) > 0 {
42+
l := len(queue)
43+
list := make([]int, 0)
44+
for i := 0; i < l; i++ {
45+
node := queue[i]
46+
list = append(list, node.Val)
47+
if node.Left != nil {
48+
queue = append(queue, node.Left)
49+
}
50+
if node.Right != nil {
51+
queue = append(queue, node.Right)
52+
}
53+
}
54+
res = append([][]int{list}, res...)
55+
queue = queue[l:]
56+
}
57+
return res
58+
}
1659
```
1760

18-
<!-- tabs:end -->
1961

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @lc app=leetcode.cn id=107 lang=golang
3+
*
4+
* [107] 二叉树的层次遍历 II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func levelOrderBottom(root *TreeNode) [][]int {
17+
var res [][]int
18+
if root == nil {
19+
return res
20+
}
21+
queue := []*TreeNode{root}
22+
23+
for len(queue) > 0 {
24+
l := len(queue)
25+
list := make([]int, 0)
26+
for i := 0; i < l; i++ {
27+
node := queue[i]
28+
list = append(list, node.Val)
29+
if node.Left != nil {
30+
queue = append(queue, node.Left)
31+
}
32+
if node.Right != nil {
33+
queue = append(queue, node.Right)
34+
}
35+
}
36+
res = append([][]int{list}, res...)
37+
queue = queue[l:]
38+
}
39+
return res
40+
}
41+
// @lc code=end
42+

‎solution/100-199/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- [104. 二叉树的最大深度 ✅](solution/100-199/0104.maximum-depth-of-binary-tree/)
1414
- [105. 从前序与中序遍历序列构造二叉树](solution/100-199/0105.construct-binary-tree-from-preorder-and-inorder-traversal/)
1515
- [106. 从中序与后序遍历序列构造二叉树](solution/100-199/0106.construct-binary-tree-from-inorder-and-postorder-traversal/)
16-
- [107. 二叉树的层次遍历 ii](solution/100-199/0107.binary-tree-level-order-traversal-ii/)
16+
- [107. 二叉树的层次遍历 ii](solution/100-199/0107.binary-tree-level-order-traversal-ii/)
1717
- [108. 将有序数组转换为二叉搜索树](solution/100-199/0108.convert-sorted-array-to-binary-search-tree/)
1818
- [109. 有序链表转换二叉搜索树](solution/100-199/0109.convert-sorted-list-to-binary-search-tree/)
1919
- [110. 平衡二叉树](solution/100-199/0110.balanced-binary-tree/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /