Solved the issue #253

This commit is contained in:
t0ur1st
2022-09-05 12:41:28 +08:00
parent 5ec282a5c7
commit 2dcb43efa1
3 changed files with 107 additions and 17 deletions

View File

@ -18,23 +18,21 @@ type TreeNode = structures.TreeNode
// 解法一 非递归
func flatten(root *TreeNode) {
list, cur := []int{}, &TreeNode{}
preorder(root, &list)
cur = root
list := preorder(root)
for i := 1; i < len(list); i++ {
cur.Left = nil
cur.Right = &TreeNode{Val: list[i], Left: nil, Right: nil}
cur = cur.Right
prev, cur := list[i-1], list[i]
prev.Left, prev.Right = nil, cur
}
return
}
func preorder(root *TreeNode, output *[]int) {
func preorder(root *TreeNode) (ans []*TreeNode) {
if root != nil {
*output = append(*output, root.Val)
preorder(root.Left, output)
preorder(root.Right, output)
ans = append(ans, root)
ans = append(ans, preorder(root.Left)...)
ans = append(ans, preorder(root.Right)...)
}
return
}
// 解法二 递归

View File

@ -15,13 +15,13 @@ type question114 struct {
// para 是参数
// one 代表第一个参数
type para114 struct {
one []int
one []string
}
// ans 是答案
// one 代表第一个答案
type ans114 struct {
one []int
one []string
}
func Test_Problem114(t *testing.T) {
@ -29,8 +29,18 @@ func Test_Problem114(t *testing.T) {
qs := []question114{
{
para114{[]int{1, 2, 3, 4, 5, 6}},
ans114{[]int{1, 2, 3, 4, 5, 6}},
para114{[]string{"1", "2", "5", "3", "4", "null", "6"}},
ans114{[]string{"1", "null", "2", "null", "3", "null", "4", "null", "5", "null", "6"}},
},
{
para114{[]string{"0"}},
ans114{[]string{"0"}},
},
{
para114{[]string{"1", "2", "3", "4", "5", "6"}},
ans114{[]string{"1", "2", "4", "5", "3", "6", "null"}},
},
}
@ -39,9 +49,10 @@ func Test_Problem114(t *testing.T) {
for _, q := range qs {
_, p := q.ans114, q.para114
fmt.Printf("【input】:%v \n", p)
rootOne := structures.Ints2TreeNode(p.one)
rootOne := structures.Strings2TreeNode(p.one)
flatten(rootOne)
fmt.Printf("【output】:%v \n", structures.Tree2Preorder(rootOne))
fmt.Printf("【levelorder output】:%v \n", structures.Tree2LevelOrderStrings(rootOne))
fmt.Printf("【preorder output】:%v \n", structures.Tree2PreOrderStrings(rootOne))
}
fmt.Printf("\n\n\n")
}

View File

@ -2,6 +2,7 @@ package structures
import (
"fmt"
"strconv"
)
// TreeNode is tree's node
@ -218,7 +219,7 @@ func Tree2ints(tn *TreeNode) []int {
return res[:i]
}
// T2s convert *TreeNode to []int
// T2s converts *TreeNode to []int
func T2s(head *TreeNode, array *[]int) {
fmt.Printf("运行到这里了 head = %v array = %v\n", head, array)
// fmt.Printf("****array = %v\n", array)
@ -231,3 +232,83 @@ func T2s(head *TreeNode, array *[]int) {
T2s(head.Right, array)
}
}
// Strings2TreeNode converts []string to *TreeNode
func Strings2TreeNode(strs []string) *TreeNode {
n := len(strs)
if n == 0 {
return nil
}
x, _ := strconv.Atoi(strs[0])
root := &TreeNode{Val: x}
queue := make([]*TreeNode, 1, n<<1)
queue[0] = root
i := 1
for i < n {
node := queue[0]
queue = queue[1:]
if i < n && strs[i] != "null" {
x, _ = strconv.Atoi(strs[i])
node.Left = &TreeNode{Val: x}
queue = append(queue, node.Left)
}
i++
if i < n && strs[i] != "null" {
x, _ = strconv.Atoi(strs[i])
node.Right = &TreeNode{Val: x}
queue = append(queue, node.Right)
}
i++
}
return root
}
// Tree2LevelOrderStrings converts *TreeNode into []string by level order traversal.
func Tree2LevelOrderStrings(root *TreeNode) []string {
var ans []string
if root == nil {
return ans
}
queue := []*TreeNode{root}
var level int
for level = 0; len(queue) > 0; level++ {
size := len(queue)
for i := 0; i < size; i++ {
node := queue[i]
if node == nil {
ans = append(ans, "null")
} else {
ans = append(ans, strconv.Itoa(node.Val))
if node.Left != nil || node.Right != nil {
queue = append(queue, node.Left, node.Right)
}
}
}
queue = queue[size:]
}
level--
return ans
}
// Tree2PreOrderStrings converts *TreeNode into []string by preorder traversal.
func Tree2PreOrderStrings(root *TreeNode) []string {
var ans []string
if root == nil {
return ans
}
stack := []*TreeNode{root}
node := root
for len(stack) > 0 {
if node == nil {
ans = append(ans, "null")
}
for node != nil {
ans = append(ans, strconv.Itoa(node.Val))
stack = append(stack, node)
node = node.Left
}
node = stack[len(stack)-1].Right
stack = stack[:len(stack)-1]
}
return ans
}