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 fa8da1e

Browse files
committed
commit solution 102
1 parent bad5f27 commit fa8da1e

File tree

6 files changed

+132
-13
lines changed

6 files changed

+132
-13
lines changed

‎index-tags.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
8383
| --- | --- | --- | --- | --- |
8484
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
85+
| [103](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal) | [二叉树的锯齿形层次遍历](/solution/100-199/0103.binary-tree-zigzag-level-order-traversal/) | ``,``,`广度优先搜索` | <font color=blue>中等</font> ||
8586

8687
#### ****
8788

@@ -149,8 +150,13 @@
149150
| --- | --- | --- | --- | --- |
150151
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
151152
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
153+
| [102](https://leetcode-cn.com/problems/binary-tree-level-order-traversal) | [二叉树的层次遍历](/solution/100-199/0102.binary-tree-level-order-traversal/) | ``,`广度优先搜索` | <font color=blue>中等</font> ||
154+
| [103](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal) | [二叉树的锯齿形层次遍历](/solution/100-199/0103.binary-tree-zigzag-level-order-traversal/) | ``,``,`广度优先搜索` | <font color=blue>中等</font> ||
152155
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
153156
| [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> ||
157+
| [111](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree) | [二叉树的最小深度](/solution/100-199/0111.minimum-depth-of-binary-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
158+
| [112](https://leetcode-cn.com/problems/path-sum) | [路径总和](/solution/100-199/0112.path-sum/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
159+
| [199](https://leetcode-cn.com/problems/binary-tree-right-side-view) | [二叉树的右视图](/solution/100-199/0199.binary-tree-right-side-view/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=blue>中等</font> ||
154160

155161
#### **递归**
156162

@@ -161,13 +167,20 @@
161167
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
162168
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
163169
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
170+
| [111](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree) | [二叉树的最小深度](/solution/100-199/0111.minimum-depth-of-binary-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
171+
| [112](https://leetcode-cn.com/problems/path-sum) | [路径总和](/solution/100-199/0112.path-sum/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
172+
| [199](https://leetcode-cn.com/problems/binary-tree-right-side-view) | [二叉树的右视图](/solution/100-199/0199.binary-tree-right-side-view/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=blue>中等</font> ||
164173

165174
#### **广度优先搜索**
166175

167176
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
168177
| --- | --- | --- | --- | --- |
169178
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
179+
| [102](https://leetcode-cn.com/problems/binary-tree-level-order-traversal) | [二叉树的层次遍历](/solution/100-199/0102.binary-tree-level-order-traversal/) | ``,`广度优先搜索` | <font color=blue>中等</font> ||
180+
| [103](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal) | [二叉树的锯齿形层次遍历](/solution/100-199/0103.binary-tree-zigzag-level-order-traversal/) | ``,``,`广度优先搜索` | <font color=blue>中等</font> ||
170181
| [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> ||
182+
| [111](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree) | [二叉树的最小深度](/solution/100-199/0111.minimum-depth-of-binary-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
183+
| [199](https://leetcode-cn.com/problems/binary-tree-right-side-view) | [二叉树的右视图](/solution/100-199/0199.binary-tree-right-side-view/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=blue>中等</font> ||
171184

172185
#### **贪心算法**
173186

‎index-type.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
| [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> |
36-
| [111](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree) | [二叉树的最小深度](/solution/100-199/0111.minimum-depth-of-binary-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> |
37-
| [112](https://leetcode-cn.com/problems/path-sum) | [路径总和](/solution/100-199/0112.path-sum/) | ``,`深度优先搜索` | <font color=green>简单</font> |
36+
| [111](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree) | [二叉树的最小深度](/solution/100-199/0111.minimum-depth-of-binary-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
37+
| [112](https://leetcode-cn.com/problems/path-sum) | [路径总和](/solution/100-199/0112.path-sum/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
3838
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> ||
3939
| [119](https://leetcode-cn.com/problems/pascals-triangle-ii) | [杨辉三角 ii](/solution/100-199/0119.pascal%27s-triangle-ii/) | `数组` | <font color=green>简单</font> |
4040
| [121](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) | [买卖股票的最佳时机](/solution/100-199/0121.best-time-to-buy-and-sell-stock/) | `数组`,`动态规划` | <font color=green>简单</font> |
@@ -461,8 +461,8 @@
461461
| [95](https://leetcode-cn.com/problems/unique-binary-search-trees-ii) | [不同的二叉搜索树 ii](/solution/1-99/0095.unique-binary-search-trees-ii/) | ``,`动态规划` | <font color=blue>中等</font> |
462462
| [96](https://leetcode-cn.com/problems/unique-binary-search-trees) | [不同的二叉搜索树](/solution/1-99/0096.unique-binary-search-trees/) | ``,`动态规划` | <font color=blue>中等</font> |
463463
| [98](https://leetcode-cn.com/problems/validate-binary-search-tree) | [验证二叉搜索树](/solution/1-99/0098.validate-binary-search-tree/) | ``,`深度优先搜索` | <font color=blue>中等</font> |
464-
| [102](https://leetcode-cn.com/problems/binary-tree-level-order-traversal) | [二叉树的层次遍历](/solution/100-199/0102.binary-tree-level-order-traversal/) | ``,`广度优先搜索` | <font color=blue>中等</font> |
465-
| [103](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal) | [二叉树的锯齿形层次遍历](/solution/100-199/0103.binary-tree-zigzag-level-order-traversal/) | ``,``,`广度优先搜索` | <font color=blue>中等</font> |
464+
| [102](https://leetcode-cn.com/problems/binary-tree-level-order-traversal) | [二叉树的层次遍历](/solution/100-199/0102.binary-tree-level-order-traversal/) | ``,`广度优先搜索` | <font color=blue>中等</font> ||
465+
| [103](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal) | [二叉树的锯齿形层次遍历](/solution/100-199/0103.binary-tree-zigzag-level-order-traversal/) | ``,``,`广度优先搜索` | <font color=blue>中等</font> ||
466466
| [105](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal) | [从前序与中序遍历序列构造二叉树](/solution/100-199/0105.construct-binary-tree-from-preorder-and-inorder-traversal/) | ``,`深度优先搜索`,`数组` | <font color=blue>中等</font> |
467467
| [106](https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal) | [从中序与后序遍历序列构造二叉树](/solution/100-199/0106.construct-binary-tree-from-inorder-and-postorder-traversal/) | ``,`深度优先搜索`,`数组` | <font color=blue>中等</font> |
468468
| [109](https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree) | [有序链表转换二叉搜索树](/solution/100-199/0109.convert-sorted-list-to-binary-search-tree/) | `深度优先搜索`,`链表` | <font color=blue>中等</font> |
@@ -507,7 +507,7 @@
507507
| [187](https://leetcode-cn.com/problems/repeated-dna-sequences) | [重复的dna序列](/solution/100-199/0187.repeated-dna-sequences/) | `位运算`,`哈希表` | <font color=blue>中等</font> ||
508508
| [192](https://leetcode-cn.com/problems/word-frequency) | [统计词频](/solution/100-199/0192.word-frequency/) | | <font color=blue>中等</font> |
509509
| [194](https://leetcode-cn.com/problems/transpose-file) | [转置文件](/solution/100-199/0194.transpose-file/) | | <font color=blue>中等</font> |
510-
| [199](https://leetcode-cn.com/problems/binary-tree-right-side-view) | [二叉树的右视图](/solution/100-199/0199.binary-tree-right-side-view/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=blue>中等</font> |
510+
| [199](https://leetcode-cn.com/problems/binary-tree-right-side-view) | [二叉树的右视图](/solution/100-199/0199.binary-tree-right-side-view/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=blue>中等</font> ||
511511
| [200](https://leetcode-cn.com/problems/number-of-islands) | [岛屿数量](/solution/200-299/0200.number-of-islands/) | `深度优先搜索`,`广度优先搜索`,`并查集` | <font color=blue>中等</font> |
512512
| [201](https://leetcode-cn.com/problems/bitwise-and-of-numbers-range) | [数字范围按位与](/solution/200-299/0201.bitwise-and-of-numbers-range/) | `位运算` | <font color=blue>中等</font> |
513513
| [207](https://leetcode-cn.com/problems/course-schedule) | [课程表](/solution/200-299/0207.course-schedule/) | `深度优先搜索`,`广度优先搜索`,``,`拓扑排序` | <font color=blue>中等</font> |

‎solution/100-199/0100.same-tree/solution.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ package leetcode
1414
* Right *TreeNode
1515
* }
1616
*/
17+
type TreeNode struct {
18+
Val int
19+
Left *TreeNode
20+
Right *TreeNode
21+
}
22+
1723
func isSameTree(p *TreeNode, q *TreeNode) bool {
1824
if p == nil && q == nil {
1925
return true
Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,69 @@
1-
# [100. xxx](https://leetcode-cn.com/problems/recover-binary-search-tree)
1+
# [102. 二叉树的层序遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/)
22

33
### 题目描述
44

5+
<p>给你一个二叉树,请你返回其按 <strong>层序遍历</strong> 得到的节点值。 (即逐层地,从左到右访问所有节点)。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>示例:</strong><br>
10+
二叉树:<code>[3,9,20,null,null,15,7]</code>,</p>
11+
12+
<pre> 3
13+
/ \
14+
9 20
15+
/ \
16+
15 7
17+
</pre>
18+
19+
<p>返回其层次遍历结果:</p>
20+
21+
<pre>[
22+
[3],
23+
[9,20],
24+
[15,7]
25+
]
26+
</pre>
527

628
### 解题思路
729

30+
1. BFS
831

932
### 具体解法
1033

11-
<!-- tabs:start -->
1234

1335
#### **Golang**
1436
```go
37+
type TreeNode struct {
38+
Val int
39+
Left *TreeNode
40+
Right *TreeNode
41+
}
1542

43+
func levelOrder(root *TreeNode) [][]int {
44+
res := [][]int{}
45+
if root == nil {
46+
return res
47+
}
48+
queue := []*TreeNode{root}
49+
for len(queue) > 0 {
50+
l := len(queue)
51+
list := []int{}
52+
for i := 0; i < l; i++ {
53+
node := queue[i]
54+
list = append(list, node.Val)
55+
if node.Left != nil {
56+
queue = append(queue, node.Left)
57+
}
58+
if node.Right != nil {
59+
queue = append(queue, node.Right)
60+
}
61+
}
62+
res = append(res, list)
63+
queue = queue[l:]
64+
}
65+
return res
66+
}
1667
```
1768

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=102 lang=golang
5+
*
6+
* [102] 二叉树的层序遍历
7+
*/
8+
9+
// @lc code=start
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+
type TreeNode struct {
20+
Val int
21+
Left *TreeNode
22+
Right *TreeNode
23+
}
24+
25+
func levelOrder(root *TreeNode) [][]int {
26+
res := [][]int{}
27+
if root == nil {
28+
return res
29+
}
30+
queue := []*TreeNode{root}
31+
for len(queue) > 0 {
32+
l := len(queue)
33+
list := []int{}
34+
for i := 0; i < l; i++ {
35+
node := queue[i]
36+
list = append(list, node.Val)
37+
if node.Left != nil {
38+
queue = append(queue, node.Left)
39+
}
40+
if node.Right != nil {
41+
queue = append(queue, node.Right)
42+
}
43+
}
44+
res = append(res, list)
45+
queue = queue[l:]
46+
}
47+
return res
48+
}
49+
50+
// @lc code=end

‎solution/100-199/_sidebar.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
- [100 - 199题](index-tags.md)
99
- [100. 相同的树 ✅](solution/100-199/0100.same-tree/)
1010
- [101. 对称二叉树 ✅](solution/100-199/0101.symmetric-tree/)
11-
- [102. 二叉树的层序遍历](solution/100-199/0102.binary-tree-level-order-traversal/)
12-
- [103. 二叉树的锯齿形层次遍历](solution/100-199/0103.binary-tree-zigzag-level-order-traversal/)
11+
- [102. 二叉树的层序遍历](solution/100-199/0102.binary-tree-level-order-traversal/)
12+
- [103. 二叉树的锯齿形层次遍历](solution/100-199/0103.binary-tree-zigzag-level-order-traversal/)
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/)
1616
- [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/)
20-
- [111. 二叉树的最小深度](solution/100-199/0111.minimum-depth-of-binary-tree/)
21-
- [112. 路径总和](solution/100-199/0112.path-sum/)
20+
- [111. 二叉树的最小深度](solution/100-199/0111.minimum-depth-of-binary-tree/)
21+
- [112. 路径总和](solution/100-199/0112.path-sum/)
2222
- [113. 路径总和 ii](solution/100-199/0113.path-sum-ii/)
2323
- [114. 二叉树展开为链表](solution/100-199/0114.flatten-binary-tree-to-linked-list/)
2424
- [115. 不同的子序列](solution/100-199/0115.distinct-subsequences/)
@@ -105,7 +105,7 @@
105105
- [196. 删除重复的电子邮箱](solution/100-199/0196.delete-duplicate-emails/)
106106
- [197. 上升的温度](solution/100-199/0197.rising-temperature/)
107107
- [198. 打家劫舍](solution/100-199/0198.house-robber/)
108-
- [199. 二叉树的右视图](solution/100-199/0199.binary-tree-right-side-view/)
108+
- [199. 二叉树的右视图](solution/100-199/0199.binary-tree-right-side-view/)
109109

110110

111111

0 commit comments

Comments
(0)

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