Skip to content

Commit

Permalink
Add solution 0589
Browse files Browse the repository at this point in the history
  • Loading branch information
halfrost committed Apr 21, 2021
1 parent 4e6a1e3 commit ebd8951
Show file tree
Hide file tree
Showing 26 changed files with 876 additions and 563 deletions.
800 changes: 400 additions & 400 deletions README.md

Large diffs are not rendered by default.

@@ -0,0 +1,43 @@
package leetcode

// Definition for a Node.
type Node struct {
Val int
Children []*Node
}

// 解法一 非递归
func preorder(root *Node) []int {
res := []int{}
if root == nil {
return res
}
stack := []*Node{root}
for len(stack) > 0 {
r := stack[len(stack)-1]
stack = stack[:len(stack)-1]
res = append(res, r.Val)
tmp := []*Node{}
for _, v := range r.Children {
tmp = append([]*Node{v}, tmp...) // 逆序存点
}
stack = append(stack, tmp...)
}
return res
}

// 解法二 递归
func preorder1(root *Node) []int {
res := []int{}
preorderdfs(root, &res)
return res
}

func preorderdfs(root *Node, res *[]int) {
if root != nil {
*res = append(*res, root.Val)
for i := 0; i < len(root.Children); i++ {
preorderdfs(root.Children[i], res)
}
}
}
@@ -0,0 +1,82 @@
package leetcode

import (
"fmt"
"testing"

"github.com/halfrost/LeetCode-Go/structures"
)

type question589 struct {
para589
ans589
}

// para 是参数
// one 代表第一个参数
type para589 struct {
one []int
}

// ans 是答案
// one 代表第一个答案
type ans589 struct {
one []int
}

func Test_Problem589(t *testing.T) {

qs := []question589{

{
para589{[]int{1, structures.NULL, 3, 2, 4, structures.NULL, 5, 6}},
ans589{[]int{1, 3, 5, 6, 2, 4}},
},

{
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}},
ans589{[]int{1, 2, 3, 6, 7, 11, 14, 4, 8, 12, 5, 9, 13, 10}},
},
}

fmt.Printf("------------------------Leetcode Problem 589------------------------\n")

for _, q := range qs {
_, p := q.ans589, q.para589
fmt.Printf("【input】:%v ", p)
rootOne := int2NaryNode(p.one)
fmt.Printf("【output】:%v \n", preorder(rootOne))
}
fmt.Printf("\n\n\n")
}

func int2NaryNode(nodes []int) *Node {
root := &Node{}
if len(nodes) > 1 {
root.Val = nodes[0]
}
queue := []*Node{}
queue = append(queue, root)
i := 1
count := 0
for i < len(nodes) {
node := queue[0]

childrens := []*Node{}
for ; i < len(nodes) && nodes[i] != structures.NULL; i++ {
tmp := &Node{Val: nodes[i]}
childrens = append(childrens, tmp)
queue = append(queue, tmp)
}
count++
if count%2 == 0 {
queue = queue[1:]
count = 1
}
if node != nil {
node.Children = childrens
}
i++
}
return root
}
90 changes: 90 additions & 0 deletions leetcode/0589.N-ary-Tree-Preorder-Traversal/README.md
@@ -0,0 +1,90 @@
# [589. N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)

## 题目

Given the `root` of an n-ary tree, return *the preorder traversal of its nodes' values*.

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

**Example 1:**

![https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)

```
Input: root = [1,null,3,2,4,null,5,6]
Output: [1,3,5,6,2,4]
```

**Example 2:**

![https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)

```
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]
Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
```

**Constraints:**

- The number of nodes in the tree is in the range `[0, 104]`.
- `0 <= Node.val <= 10^4`
- The height of the n-ary tree is less than or equal to `1000`.

**Follow up:** Recursive solution is trivial, could you do it iteratively?

## 题目大意

给定一个 N 叉树,返回其节点值的 **前序遍历** 。N 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 `null` 分隔(请参见示例)。

## 解题思路

- N 叉树和二叉树的前序遍历原理完全一样。二叉树非递归解法需要用到栈辅助,N 叉树同样如此。将父节点的所有孩子节点**逆序**入栈,逆序的目的是为了让前序节点永远在栈顶。依次循环直到栈里所有元素都出栈。输出的结果即为 N 叉树的前序遍历。时间复杂度 O(n),空间复杂度 O(n)。
- 递归解法非常简单,见解法二。

## 代码

```go
package leetcode

// Definition for a Node.
type Node struct {
Val int
Children []*Node
}

// 解法一 非递归
func preorder(root *Node) []int {
res := []int{}
if root == nil {
return res
}
stack := []*Node{root}
for len(stack) > 0 {
r := stack[len(stack)-1]
stack = stack[:len(stack)-1]
res = append(res, r.Val)
tmp := []*Node{}
for _, v := range r.Children {
tmp = append([]*Node{v}, tmp...) // 逆序存点
}
stack = append(stack, tmp...)
}
return res
}

// 解法二 递归
func preorder1(root *Node) []int {
res := []int{}
preorderdfs(root, &res)
return res
}

func preorderdfs(root *Node, res *[]int) {
if root != nil {
*res = append(*res, root.Val)
for i := 0; i < len(root.Children); i++ {
preorderdfs(root.Children[i], res)
}
}
}
```
Expand Up @@ -110,5 +110,5 @@ func min(a, b int) int {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0575.Distribute-Candies/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0594.Longest-Harmonious-Subsequence/">下一页➡️</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0589.N-ary-Tree-Preorder-Traversal/">下一页➡️</a></p>
</div>
@@ -0,0 +1,97 @@
# [589. N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)

## 题目

Given the `root` of an n-ary tree, return *the preorder traversal of its nodes' values*.

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

**Example 1:**

![https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)

```
Input: root = [1,null,3,2,4,null,5,6]
Output: [1,3,5,6,2,4]
```

**Example 2:**

![https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)

```
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]
Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
```

**Constraints:**

- The number of nodes in the tree is in the range `[0, 104]`.
- `0 <= Node.val <= 10^4`
- The height of the n-ary tree is less than or equal to `1000`.

**Follow up:** Recursive solution is trivial, could you do it iteratively?

## 题目大意

给定一个 N 叉树,返回其节点值的 **前序遍历** 。N 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 `null` 分隔(请参见示例)。

## 解题思路

- N 叉树和二叉树的前序遍历原理完全一样。二叉树非递归解法需要用到栈辅助,N 叉树同样如此。将父节点的所有孩子节点**逆序**入栈,逆序的目的是为了让前序节点永远在栈顶。依次循环直到栈里所有元素都出栈。输出的结果即为 N 叉树的前序遍历。时间复杂度 O(n),空间复杂度 O(n)。
- 递归解法非常简单,见解法二。

## 代码

```go
package leetcode

// Definition for a Node.
type Node struct {
Val int
Children []*Node
}

// 解法一 非递归
func preorder(root *Node) []int {
res := []int{}
if root == nil {
return res
}
stack := []*Node{root}
for len(stack) > 0 {
r := stack[len(stack)-1]
stack = stack[:len(stack)-1]
res = append(res, r.Val)
tmp := []*Node{}
for _, v := range r.Children {
tmp = append([]*Node{v}, tmp...) // 逆序存点
}
stack = append(stack, tmp...)
}
return res
}

// 解法二 递归
func preorder1(root *Node) []int {
res := []int{}
preorderdfs(root, &res)
return res
}

func preorderdfs(root *Node, res *[]int) {
if root != nil {
*res = append(*res, root.Val)
for i := 0; i < len(root.Children); i++ {
preorderdfs(root.Children[i], res)
}
}
}
```


----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0581.Shortest-Unsorted-Continuous-Subarray/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0594.Longest-Harmonious-Subsequence/">下一页➡️</a></p>
</div>
Expand Up @@ -59,6 +59,6 @@ func findLHS(nums []int) int {

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

0 comments on commit ebd8951

Please sign in to comment.