Skip to content

Commit ebd8951

Browse files
committed
Add solution 0589
1 parent 4e6a1e3 commit ebd8951

26 files changed

+876
-563
lines changed

README.md

Lines changed: 400 additions & 400 deletions
Large diffs are not rendered by default.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode
2+
3+
// Definition for a Node.
4+
type Node struct {
5+
Val int
6+
Children []*Node
7+
}
8+
9+
// 解法一 非递归
10+
func preorder(root *Node) []int {
11+
res := []int{}
12+
if root == nil {
13+
return res
14+
}
15+
stack := []*Node{root}
16+
for len(stack) > 0 {
17+
r := stack[len(stack)-1]
18+
stack = stack[:len(stack)-1]
19+
res = append(res, r.Val)
20+
tmp := []*Node{}
21+
for _, v := range r.Children {
22+
tmp = append([]*Node{v}, tmp...) // 逆序存点
23+
}
24+
stack = append(stack, tmp...)
25+
}
26+
return res
27+
}
28+
29+
// 解法二 递归
30+
func preorder1(root *Node) []int {
31+
res := []int{}
32+
preorderdfs(root, &res)
33+
return res
34+
}
35+
36+
func preorderdfs(root *Node, res *[]int) {
37+
if root != nil {
38+
*res = append(*res, root.Val)
39+
for i := 0; i < len(root.Children); i++ {
40+
preorderdfs(root.Children[i], res)
41+
}
42+
}
43+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question589 struct {
11+
para589
12+
ans589
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para589 struct {
18+
one []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans589 struct {
24+
one []int
25+
}
26+
27+
func Test_Problem589(t *testing.T) {
28+
29+
qs := []question589{
30+
31+
{
32+
para589{[]int{1, structures.NULL, 3, 2, 4, structures.NULL, 5, 6}},
33+
ans589{[]int{1, 3, 5, 6, 2, 4}},
34+
},
35+
36+
{
37+
para589{[]int{1, structures.NULL, 2, 3, 4, 5, structures.NULL, structures.NULL, 6, 7, structures.NULL, 8, structures.NULL, 9, 10, structures.NULL, structures.NULL, 11, structures.NULL, 12, structures.NULL, 13, structures.NULL, structures.NULL, 14}},
38+
ans589{[]int{1, 2, 3, 6, 7, 11, 14, 4, 8, 12, 5, 9, 13, 10}},
39+
},
40+
}
41+
42+
fmt.Printf("------------------------Leetcode Problem 589------------------------\n")
43+
44+
for _, q := range qs {
45+
_, p := q.ans589, q.para589
46+
fmt.Printf("【input】:%v ", p)
47+
rootOne := int2NaryNode(p.one)
48+
fmt.Printf("【output】:%v \n", preorder(rootOne))
49+
}
50+
fmt.Printf("\n\n\n")
51+
}
52+
53+
func int2NaryNode(nodes []int) *Node {
54+
root := &Node{}
55+
if len(nodes) > 1 {
56+
root.Val = nodes[0]
57+
}
58+
queue := []*Node{}
59+
queue = append(queue, root)
60+
i := 1
61+
count := 0
62+
for i < len(nodes) {
63+
node := queue[0]
64+
65+
childrens := []*Node{}
66+
for ; i < len(nodes) && nodes[i] != structures.NULL; i++ {
67+
tmp := &Node{Val: nodes[i]}
68+
childrens = append(childrens, tmp)
69+
queue = append(queue, tmp)
70+
}
71+
count++
72+
if count%2 == 0 {
73+
queue = queue[1:]
74+
count = 1
75+
}
76+
if node != nil {
77+
node.Children = childrens
78+
}
79+
i++
80+
}
81+
return root
82+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# [589. N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)
2+
3+
## 题目
4+
5+
Given the `root` of an n-ary tree, return *the preorder traversal of its nodes' values*.
6+
7+
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
8+
9+
**Example 1:**
10+
11+
![https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
12+
13+
```
14+
Input: root = [1,null,3,2,4,null,5,6]
15+
Output: [1,3,5,6,2,4]
16+
```
17+
18+
**Example 2:**
19+
20+
![https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
21+
22+
```
23+
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]
24+
Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
25+
```
26+
27+
**Constraints:**
28+
29+
- The number of nodes in the tree is in the range `[0, 104]`.
30+
- `0 <= Node.val <= 10^4`
31+
- The height of the n-ary tree is less than or equal to `1000`.
32+
33+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
34+
35+
## 题目大意
36+
37+
给定一个 N 叉树,返回其节点值的 **前序遍历** 。N 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 `null` 分隔(请参见示例)。
38+
39+
## 解题思路
40+
41+
- N 叉树和二叉树的前序遍历原理完全一样。二叉树非递归解法需要用到栈辅助,N 叉树同样如此。将父节点的所有孩子节点**逆序**入栈,逆序的目的是为了让前序节点永远在栈顶。依次循环直到栈里所有元素都出栈。输出的结果即为 N 叉树的前序遍历。时间复杂度 O(n),空间复杂度 O(n)。
42+
- 递归解法非常简单,见解法二。
43+
44+
## 代码
45+
46+
```go
47+
package leetcode
48+
49+
// Definition for a Node.
50+
type Node struct {
51+
Val int
52+
Children []*Node
53+
}
54+
55+
// 解法一 非递归
56+
func preorder(root *Node) []int {
57+
res := []int{}
58+
if root == nil {
59+
return res
60+
}
61+
stack := []*Node{root}
62+
for len(stack) > 0 {
63+
r := stack[len(stack)-1]
64+
stack = stack[:len(stack)-1]
65+
res = append(res, r.Val)
66+
tmp := []*Node{}
67+
for _, v := range r.Children {
68+
tmp = append([]*Node{v}, tmp...) // 逆序存点
69+
}
70+
stack = append(stack, tmp...)
71+
}
72+
return res
73+
}
74+
75+
// 解法二 递归
76+
func preorder1(root *Node) []int {
77+
res := []int{}
78+
preorderdfs(root, &res)
79+
return res
80+
}
81+
82+
func preorderdfs(root *Node, res *[]int) {
83+
if root != nil {
84+
*res = append(*res, root.Val)
85+
for i := 0; i < len(root.Children); i++ {
86+
preorderdfs(root.Children[i], res)
87+
}
88+
}
89+
}
90+
```

website/content/ChapterFour/0500~0599/0581.Shortest-Unsorted-Continuous-Subarray.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,5 @@ func min(a, b int) int {
110110
----------------------------------------------
111111
<div style="display: flex;justify-content: space-between;align-items: center;">
112112
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0575.Distribute-Candies/">⬅️上一页</a></p>
113-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0594.Longest-Harmonious-Subsequence/">下一页➡️</a></p>
113+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0589.N-ary-Tree-Preorder-Traversal/">下一页➡️</a></p>
114114
</div>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [589. N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)
2+
3+
## 题目
4+
5+
Given the `root` of an n-ary tree, return *the preorder traversal of its nodes' values*.
6+
7+
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
8+
9+
**Example 1:**
10+
11+
![https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
12+
13+
```
14+
Input: root = [1,null,3,2,4,null,5,6]
15+
Output: [1,3,5,6,2,4]
16+
```
17+
18+
**Example 2:**
19+
20+
![https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
21+
22+
```
23+
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]
24+
Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
25+
```
26+
27+
**Constraints:**
28+
29+
- The number of nodes in the tree is in the range `[0, 104]`.
30+
- `0 <= Node.val <= 10^4`
31+
- The height of the n-ary tree is less than or equal to `1000`.
32+
33+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
34+
35+
## 题目大意
36+
37+
给定一个 N 叉树,返回其节点值的 **前序遍历** 。N 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 `null` 分隔(请参见示例)。
38+
39+
## 解题思路
40+
41+
- N 叉树和二叉树的前序遍历原理完全一样。二叉树非递归解法需要用到栈辅助,N 叉树同样如此。将父节点的所有孩子节点**逆序**入栈,逆序的目的是为了让前序节点永远在栈顶。依次循环直到栈里所有元素都出栈。输出的结果即为 N 叉树的前序遍历。时间复杂度 O(n),空间复杂度 O(n)。
42+
- 递归解法非常简单,见解法二。
43+
44+
## 代码
45+
46+
```go
47+
package leetcode
48+
49+
// Definition for a Node.
50+
type Node struct {
51+
Val int
52+
Children []*Node
53+
}
54+
55+
// 解法一 非递归
56+
func preorder(root *Node) []int {
57+
res := []int{}
58+
if root == nil {
59+
return res
60+
}
61+
stack := []*Node{root}
62+
for len(stack) > 0 {
63+
r := stack[len(stack)-1]
64+
stack = stack[:len(stack)-1]
65+
res = append(res, r.Val)
66+
tmp := []*Node{}
67+
for _, v := range r.Children {
68+
tmp = append([]*Node{v}, tmp...) // 逆序存点
69+
}
70+
stack = append(stack, tmp...)
71+
}
72+
return res
73+
}
74+
75+
// 解法二 递归
76+
func preorder1(root *Node) []int {
77+
res := []int{}
78+
preorderdfs(root, &res)
79+
return res
80+
}
81+
82+
func preorderdfs(root *Node, res *[]int) {
83+
if root != nil {
84+
*res = append(*res, root.Val)
85+
for i := 0; i < len(root.Children); i++ {
86+
preorderdfs(root.Children[i], res)
87+
}
88+
}
89+
}
90+
```
91+
92+
93+
----------------------------------------------
94+
<div style="display: flex;justify-content: space-between;align-items: center;">
95+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0581.Shortest-Unsorted-Continuous-Subarray/">⬅️上一页</a></p>
96+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0594.Longest-Harmonious-Subsequence/">下一页➡️</a></p>
97+
</div>

website/content/ChapterFour/0500~0599/0594.Longest-Harmonious-Subsequence.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ func findLHS(nums []int) int {
5959

6060
----------------------------------------------
6161
<div style="display: flex;justify-content: space-between;align-items: center;">
62-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0581.Shortest-Unsorted-Continuous-Subarray/">⬅️上一页</a></p>
62+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0589.N-ary-Tree-Preorder-Traversal/">⬅️上一页</a></p>
6363
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0598.Range-Addition-II/">下一页➡️</a></p>
6464
</div>

0 commit comments

Comments
 (0)