mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-13 23:40:35 +08:00
Solved the issue #253
This commit is contained in:
@ -18,23 +18,21 @@ type TreeNode = structures.TreeNode
|
|||||||
|
|
||||||
// 解法一 非递归
|
// 解法一 非递归
|
||||||
func flatten(root *TreeNode) {
|
func flatten(root *TreeNode) {
|
||||||
list, cur := []int{}, &TreeNode{}
|
list := preorder(root)
|
||||||
preorder(root, &list)
|
|
||||||
cur = root
|
|
||||||
for i := 1; i < len(list); i++ {
|
for i := 1; i < len(list); i++ {
|
||||||
cur.Left = nil
|
prev, cur := list[i-1], list[i]
|
||||||
cur.Right = &TreeNode{Val: list[i], Left: nil, Right: nil}
|
prev.Left, prev.Right = nil, cur
|
||||||
cur = cur.Right
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func preorder(root *TreeNode, output *[]int) {
|
func preorder(root *TreeNode) (ans []*TreeNode) {
|
||||||
if root != nil {
|
if root != nil {
|
||||||
*output = append(*output, root.Val)
|
ans = append(ans, root)
|
||||||
preorder(root.Left, output)
|
ans = append(ans, preorder(root.Left)...)
|
||||||
preorder(root.Right, output)
|
ans = append(ans, preorder(root.Right)...)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解法二 递归
|
// 解法二 递归
|
||||||
|
@ -15,13 +15,13 @@ type question114 struct {
|
|||||||
// para 是参数
|
// para 是参数
|
||||||
// one 代表第一个参数
|
// one 代表第一个参数
|
||||||
type para114 struct {
|
type para114 struct {
|
||||||
one []int
|
one []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ans 是答案
|
// ans 是答案
|
||||||
// one 代表第一个答案
|
// one 代表第一个答案
|
||||||
type ans114 struct {
|
type ans114 struct {
|
||||||
one []int
|
one []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Problem114(t *testing.T) {
|
func Test_Problem114(t *testing.T) {
|
||||||
@ -29,8 +29,18 @@ func Test_Problem114(t *testing.T) {
|
|||||||
qs := []question114{
|
qs := []question114{
|
||||||
|
|
||||||
{
|
{
|
||||||
para114{[]int{1, 2, 3, 4, 5, 6}},
|
para114{[]string{"1", "2", "5", "3", "4", "null", "6"}},
|
||||||
ans114{[]int{1, 2, 3, 4, 5, 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 {
|
for _, q := range qs {
|
||||||
_, p := q.ans114, q.para114
|
_, p := q.ans114, q.para114
|
||||||
fmt.Printf("【input】:%v \n", p)
|
fmt.Printf("【input】:%v \n", p)
|
||||||
rootOne := structures.Ints2TreeNode(p.one)
|
rootOne := structures.Strings2TreeNode(p.one)
|
||||||
flatten(rootOne)
|
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")
|
fmt.Printf("\n\n\n")
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package structures
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TreeNode is tree's node
|
// TreeNode is tree's node
|
||||||
@ -218,7 +219,7 @@ func Tree2ints(tn *TreeNode) []int {
|
|||||||
return res[:i]
|
return res[:i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// T2s convert *TreeNode to []int
|
// T2s converts *TreeNode to []int
|
||||||
func T2s(head *TreeNode, array *[]int) {
|
func T2s(head *TreeNode, array *[]int) {
|
||||||
fmt.Printf("运行到这里了 head = %v array = %v\n", head, array)
|
fmt.Printf("运行到这里了 head = %v array = %v\n", head, array)
|
||||||
// fmt.Printf("****array = %v\n", array)
|
// fmt.Printf("****array = %v\n", array)
|
||||||
@ -231,3 +232,83 @@ func T2s(head *TreeNode, array *[]int) {
|
|||||||
T2s(head.Right, array)
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user