Skip to content

Commit 4268f58

Browse files
committed
Add solution 0429
1 parent a43125d commit 4268f58

20 files changed

+307
-89
lines changed

README.md

Lines changed: 60 additions & 60 deletions
Large diffs are not rendered by default.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode
2+
3+
/**
4+
* Definition for a Node.
5+
* type Node struct {
6+
* Val int
7+
* Children []*Node
8+
* }
9+
*/
10+
11+
type Node struct {
12+
Val int
13+
Children []*Node
14+
}
15+
16+
func levelOrder(root *Node) [][]int {
17+
var res [][]int
18+
var temp []int
19+
if root == nil {
20+
return res
21+
}
22+
queue := []*Node{root, nil}
23+
for len(queue) > 1 {
24+
node := queue[0]
25+
queue = queue[1:]
26+
if node == nil {
27+
queue = append(queue, nil)
28+
res = append(res, temp)
29+
temp = []int{}
30+
} else {
31+
temp = append(temp, node.Val)
32+
if len(node.Children) > 0 {
33+
queue = append(queue, node.Children...)
34+
}
35+
}
36+
}
37+
res = append(res, temp)
38+
return res
39+
}

leetcode/0429.N-ary-Tree-Level-Order-Traversal/429. N-ary Tree Level Order Traversal_test.go

Whitespace-only changes.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [429. N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)
2+
3+
4+
## 题目
5+
6+
Given an n-ary tree, return the *level order* traversal of its nodes' values.
7+
8+
*Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).*
9+
10+
**Example 1:**
11+
12+
![https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
13+
14+
```
15+
Input: root = [1,null,3,2,4,null,5,6]
16+
Output: [[1],[3,2,4],[5,6]]
17+
18+
```
19+
20+
**Example 2:**
21+
22+
![https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
23+
24+
```
25+
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
26+
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
27+
28+
```
29+
30+
**Constraints:**
31+
32+
- The height of the n-ary tree is less than or equal to `1000`
33+
- The total number of nodes is between `[0, 104]`
34+
35+
## 题目大意
36+
37+
给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。
38+
39+
## 解题思路
40+
41+
- 这是 n 叉树的系列题,第 589 题也是这一系列的题目。这一题思路不难,既然是层序遍历,用 BFS 解答。
42+
43+
## 代码
44+
45+
```go
46+
package leetcode
47+
48+
/**
49+
* Definition for a Node.
50+
* type Node struct {
51+
* Val int
52+
* Children []*Node
53+
* }
54+
*/
55+
56+
type Node struct {
57+
Val int
58+
Children []*Node
59+
}
60+
61+
func levelOrder(root *Node) [][]int {
62+
var res [][]int
63+
var temp []int
64+
if root == nil {
65+
return res
66+
}
67+
queue := []*Node{root, nil}
68+
for len(queue) > 1 {
69+
node := queue[0]
70+
queue = queue[1:]
71+
if node == nil {
72+
queue = append(queue, nil)
73+
res = append(res, temp)
74+
temp = []int{}
75+
} else {
76+
temp = append(temp, node.Val)
77+
if len(node.Children) > 0 {
78+
queue = append(queue, node.Children...)
79+
}
80+
}
81+
}
82+
res = append(res, temp)
83+
return res
84+
}
85+
```

website/content/ChapterFour/0400~0499/0424.Longest-Repeating-Character-Replacement.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ func max(a int, b int) int {
8888
----------------------------------------------
8989
<div style="display: flex;justify-content: space-between;align-items: center;">
9090
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0423.Reconstruct-Original-Digits-from-English/">⬅️上一页</a></p>
91-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0433.Minimum-Genetic-Mutation/">下一页➡️</a></p>
91+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0429.N-ary-Tree-Level-Order-Traversal/">下一页➡️</a></p>
9292
</div>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [429.N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)
2+
3+
4+
## 题目
5+
6+
Given an n-ary tree, return the *level order* traversal of its nodes' values.
7+
8+
*Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).*
9+
10+
**Example 1:**
11+
12+
![https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
13+
14+
```
15+
Input: root = [1,null,3,2,4,null,5,6]
16+
Output: [[1],[3,2,4],[5,6]]
17+
18+
```
19+
20+
**Example 2:**
21+
22+
![https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
23+
24+
```
25+
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
26+
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
27+
28+
```
29+
30+
**Constraints:**
31+
32+
- The height of the n-ary tree is less than or equal to `1000`
33+
- The total number of nodes is between `[0, 104]`
34+
35+
## 题目大意
36+
37+
给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。
38+
39+
## 解题思路
40+
41+
- 这是 n 叉树的系列题,第 589 题也是这一系列的题目。这一题思路不难,既然是层序遍历,用 BFS 解答。
42+
43+
## 代码
44+
45+
```go
46+
package leetcode
47+
48+
/**
49+
* Definition for a Node.
50+
* type Node struct {
51+
* Val int
52+
* Children []*Node
53+
* }
54+
*/
55+
56+
type Node struct {
57+
Val int
58+
Children []*Node
59+
}
60+
61+
func levelOrder(root *Node) [][]int {
62+
var res [][]int
63+
var temp []int
64+
if root == nil {
65+
return res
66+
}
67+
queue := []*Node{root, nil}
68+
for len(queue) > 1 {
69+
node := queue[0]
70+
queue = queue[1:]
71+
if node == nil {
72+
queue = append(queue, nil)
73+
res = append(res, temp)
74+
temp = []int{}
75+
} else {
76+
temp = append(temp, node.Val)
77+
if len(node.Children) > 0 {
78+
queue = append(queue, node.Children...)
79+
}
80+
}
81+
}
82+
res = append(res, temp)
83+
return res
84+
}
85+
```
86+
87+
88+
----------------------------------------------
89+
<div style="display: flex;justify-content: space-between;align-items: center;">
90+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0424.Longest-Repeating-Character-Replacement/">⬅️上一页</a></p>
91+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0433.Minimum-Genetic-Mutation/">下一页➡️</a></p>
92+
</div>

website/content/ChapterFour/0400~0499/0433.Minimum-Genetic-Mutation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ func convert(gene string) uint32 {
185185

186186
----------------------------------------------
187187
<div style="display: flex;justify-content: space-between;align-items: center;">
188-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0424.Longest-Repeating-Character-Replacement/">⬅️上一页</a></p>
188+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0429.N-ary-Tree-Level-Order-Traversal/">⬅️上一页</a></p>
189189
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0435.Non-overlapping-Intervals/">下一页➡️</a></p>
190190
</div>

website/content/ChapterTwo/Array.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ weight: 1
191191
|0713|Subarray Product Less Than K|[Go]({{< relref "/ChapterFour/0700~0799/0713.Subarray-Product-Less-Than-K.md" >}})|Medium| O(n)| O(1)||41.2%|
192192
|0714|Best Time to Buy and Sell Stock with Transaction Fee|[Go]({{< relref "/ChapterFour/0700~0799/0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee.md" >}})|Medium| O(n)| O(1)||59.0%|
193193
|0717|1-bit and 2-bit Characters|[Go]({{< relref "/ChapterFour/0700~0799/0717.1-bit-and-2-bit-Characters.md" >}})|Easy||||46.3%|
194-
|0718|Maximum Length of Repeated Subarray|[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})|Medium||||51.0%|
194+
|0718|Maximum Length of Repeated Subarray|[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})|Medium||||51.1%|
195195
|0719|Find K-th Smallest Pair Distance|[Go]({{< relref "/ChapterFour/0700~0799/0719.Find-K-th-Smallest-Pair-Distance.md" >}})|Hard||||33.2%|
196196
|0720|Longest Word in Dictionary|[Go]({{< relref "/ChapterFour/0700~0799/0720.Longest-Word-in-Dictionary.md" >}})|Medium||||49.9%|
197197
|0721|Accounts Merge|[Go]({{< relref "/ChapterFour/0700~0799/0721.Accounts-Merge.md" >}})|Medium||||53.4%|
@@ -250,7 +250,7 @@ weight: 1
250250
|0927|Three Equal Parts|[Go]({{< relref "/ChapterFour/0900~0999/0927.Three-Equal-Parts.md" >}})|Hard||||39.1%|
251251
|0928|Minimize Malware Spread II|[Go]({{< relref "/ChapterFour/0900~0999/0928.Minimize-Malware-Spread-II.md" >}})|Hard||||41.6%|
252252
|0930|Binary Subarrays With Sum|[Go]({{< relref "/ChapterFour/0900~0999/0930.Binary-Subarrays-With-Sum.md" >}})|Medium||||46.3%|
253-
|0942|DI String Match|[Go]({{< relref "/ChapterFour/0900~0999/0942.DI-String-Match.md" >}})|Easy||||74.5%|
253+
|0942|DI String Match|[Go]({{< relref "/ChapterFour/0900~0999/0942.DI-String-Match.md" >}})|Easy||||74.6%|
254254
|0946|Validate Stack Sequences|[Go]({{< relref "/ChapterFour/0900~0999/0946.Validate-Stack-Sequences.md" >}})|Medium||||64.6%|
255255
|0952|Largest Component Size by Common Factor|[Go]({{< relref "/ChapterFour/0900~0999/0952.Largest-Component-Size-by-Common-Factor.md" >}})|Hard||||36.6%|
256256
|0953|Verifying an Alien Dictionary|[Go]({{< relref "/ChapterFour/0900~0999/0953.Verifying-an-Alien-Dictionary.md" >}})|Easy||||52.1%|

website/content/ChapterTwo/Backtracking.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func updateMatrix_BFS(matrix [][]int) [][]int {
130130
|0526|Beautiful Arrangement|[Go]({{< relref "/ChapterFour/0500~0599/0526.Beautiful-Arrangement.md" >}})|Medium| O(n^2)| O(1)|❤️|62.5%|
131131
|0638|Shopping Offers|[Go]({{< relref "/ChapterFour/0600~0699/0638.Shopping-Offers.md" >}})|Medium||||53.9%|
132132
|0784|Letter Case Permutation|[Go]({{< relref "/ChapterFour/0700~0799/0784.Letter-Case-Permutation.md" >}})|Medium| O(n)| O(n)||69.5%|
133-
|0816|Ambiguous Coordinates|[Go]({{< relref "/ChapterFour/0800~0899/0816.Ambiguous-Coordinates.md" >}})|Medium||||55.7%|
133+
|0816|Ambiguous Coordinates|[Go]({{< relref "/ChapterFour/0800~0899/0816.Ambiguous-Coordinates.md" >}})|Medium||||55.6%|
134134
|0842|Split Array into Fibonacci Sequence|[Go]({{< relref "/ChapterFour/0800~0899/0842.Split-Array-into-Fibonacci-Sequence.md" >}})|Medium| O(n^2)| O(1)|❤️|37.2%|
135135
|0980|Unique Paths III|[Go]({{< relref "/ChapterFour/0900~0999/0980.Unique-Paths-III.md" >}})|Hard| O(n log n)| O(n)||77.3%|
136136
|0996|Number of Squareful Arrays|[Go]({{< relref "/ChapterFour/0900~0999/0996.Number-of-Squareful-Arrays.md" >}})|Hard| O(n log n)| O(n) ||48.7%|

website/content/ChapterTwo/Binary_Search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func peakIndexInMountainArray(A []int) int {
173173
|0668|Kth Smallest Number in Multiplication Table|[Go]({{< relref "/ChapterFour/0600~0699/0668.Kth-Smallest-Number-in-Multiplication-Table.md" >}})|Hard||||48.5%|
174174
|0704|Binary Search|[Go]({{< relref "/ChapterFour/0700~0799/0704.Binary-Search.md" >}})|Easy||||54.9%|
175175
|0710|Random Pick with Blacklist|[Go]({{< relref "/ChapterFour/0700~0799/0710.Random-Pick-with-Blacklist.md" >}})|Hard| O(n)| O(n) ||33.3%|
176-
|0718|Maximum Length of Repeated Subarray|[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})|Medium||||51.0%|
176+
|0718|Maximum Length of Repeated Subarray|[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})|Medium||||51.1%|
177177
|0719|Find K-th Smallest Pair Distance|[Go]({{< relref "/ChapterFour/0700~0799/0719.Find-K-th-Smallest-Pair-Distance.md" >}})|Hard||||33.2%|
178178
|0744|Find Smallest Letter Greater Than Target|[Go]({{< relref "/ChapterFour/0700~0799/0744.Find-Smallest-Letter-Greater-Than-Target.md" >}})|Easy||||45.8%|
179179
|0778|Swim in Rising Water|[Go]({{< relref "/ChapterFour/0700~0799/0778.Swim-in-Rising-Water.md" >}})|Hard||||57.8%|

0 commit comments

Comments
 (0)