Skip to content

Commit 2dcb43e

Browse files
committed
Solved the issue halfrost#253
1 parent 5ec282a commit 2dcb43e

File tree

3 files changed

+107
-17
lines changed

3 files changed

+107
-17
lines changed

leetcode/0114.Flatten-Binary-Tree-to-Linked-List/114. Flatten Binary Tree to Linked List.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,21 @@ type TreeNode = structures.TreeNode
1818

1919
// 解法一 非递归
2020
func flatten(root *TreeNode) {
21-
list, cur := []int{}, &TreeNode{}
22-
preorder(root, &list)
23-
cur = root
21+
list := preorder(root)
2422
for i := 1; i < len(list); i++ {
25-
cur.Left = nil
26-
cur.Right = &TreeNode{Val: list[i], Left: nil, Right: nil}
27-
cur = cur.Right
23+
prev, cur := list[i-1], list[i]
24+
prev.Left, prev.Right = nil, cur
2825
}
2926
return
3027
}
3128

32-
func preorder(root *TreeNode, output *[]int) {
29+
func preorder(root *TreeNode) (ans []*TreeNode) {
3330
if root != nil {
34-
*output = append(*output, root.Val)
35-
preorder(root.Left, output)
36-
preorder(root.Right, output)
31+
ans = append(ans, root)
32+
ans = append(ans, preorder(root.Left)...)
33+
ans = append(ans, preorder(root.Right)...)
3734
}
35+
return
3836
}
3937

4038
// 解法二 递归

leetcode/0114.Flatten-Binary-Tree-to-Linked-List/114. Flatten Binary Tree to Linked List_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,32 @@ type question114 struct {
1515
// para 是参数
1616
// one 代表第一个参数
1717
type para114 struct {
18-
one []int
18+
one []string
1919
}
2020

2121
// ans 是答案
2222
// one 代表第一个答案
2323
type ans114 struct {
24-
one []int
24+
one []string
2525
}
2626

2727
func Test_Problem114(t *testing.T) {
2828

2929
qs := []question114{
3030

3131
{
32-
para114{[]int{1, 2, 3, 4, 5, 6}},
33-
ans114{[]int{1, 2, 3, 4, 5, 6}},
32+
para114{[]string{"1", "2", "5", "3", "4", "null", "6"}},
33+
ans114{[]string{"1", "null", "2", "null", "3", "null", "4", "null", "5", "null", "6"}},
34+
},
35+
36+
{
37+
para114{[]string{"0"}},
38+
ans114{[]string{"0"}},
39+
},
40+
41+
{
42+
para114{[]string{"1", "2", "3", "4", "5", "6"}},
43+
ans114{[]string{"1", "2", "4", "5", "3", "6", "null"}},
3444
},
3545
}
3646

@@ -39,9 +49,10 @@ func Test_Problem114(t *testing.T) {
3949
for _, q := range qs {
4050
_, p := q.ans114, q.para114
4151
fmt.Printf("【input】:%v \n", p)
42-
rootOne := structures.Ints2TreeNode(p.one)
52+
rootOne := structures.Strings2TreeNode(p.one)
4353
flatten(rootOne)
44-
fmt.Printf("【output】:%v \n", structures.Tree2Preorder(rootOne))
54+
fmt.Printf("【levelorder output】:%v \n", structures.Tree2LevelOrderStrings(rootOne))
55+
fmt.Printf("【preorder output】:%v \n", structures.Tree2PreOrderStrings(rootOne))
4556
}
4657
fmt.Printf("\n\n\n")
4758
}

structures/TreeNode.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package structures
22

33
import (
44
"fmt"
5+
"strconv"
56
)
67

78
// TreeNode is tree's node
@@ -218,7 +219,7 @@ func Tree2ints(tn *TreeNode) []int {
218219
return res[:i]
219220
}
220221

221-
// T2s convert *TreeNode to []int
222+
// T2s converts *TreeNode to []int
222223
func T2s(head *TreeNode, array *[]int) {
223224
fmt.Printf("运行到这里了 head = %v array = %v\n", head, array)
224225
// fmt.Printf("****array = %v\n", array)
@@ -231,3 +232,83 @@ func T2s(head *TreeNode, array *[]int) {
231232
T2s(head.Right, array)
232233
}
233234
}
235+
236+
// Strings2TreeNode converts []string to *TreeNode
237+
func Strings2TreeNode(strs []string) *TreeNode {
238+
n := len(strs)
239+
if n == 0 {
240+
return nil
241+
}
242+
x, _ := strconv.Atoi(strs[0])
243+
root := &TreeNode{Val: x}
244+
queue := make([]*TreeNode, 1, n<<1)
245+
queue[0] = root
246+
i := 1
247+
for i < n {
248+
node := queue[0]
249+
queue = queue[1:]
250+
if i < n && strs[i] != "null" {
251+
x, _ = strconv.Atoi(strs[i])
252+
node.Left = &TreeNode{Val: x}
253+
queue = append(queue, node.Left)
254+
}
255+
i++
256+
if i < n && strs[i] != "null" {
257+
x, _ = strconv.Atoi(strs[i])
258+
node.Right = &TreeNode{Val: x}
259+
queue = append(queue, node.Right)
260+
}
261+
i++
262+
}
263+
return root
264+
}
265+
266+
// Tree2LevelOrderStrings converts *TreeNode into []string by level order traversal.
267+
func Tree2LevelOrderStrings(root *TreeNode) []string {
268+
var ans []string
269+
if root == nil {
270+
return ans
271+
}
272+
queue := []*TreeNode{root}
273+
var level int
274+
for level = 0; len(queue) > 0; level++ {
275+
size := len(queue)
276+
for i := 0; i < size; i++ {
277+
node := queue[i]
278+
if node == nil {
279+
ans = append(ans, "null")
280+
} else {
281+
ans = append(ans, strconv.Itoa(node.Val))
282+
if node.Left != nil || node.Right != nil {
283+
queue = append(queue, node.Left, node.Right)
284+
}
285+
}
286+
}
287+
queue = queue[size:]
288+
}
289+
level--
290+
return ans
291+
}
292+
293+
// Tree2PreOrderStrings converts *TreeNode into []string by preorder traversal.
294+
func Tree2PreOrderStrings(root *TreeNode) []string {
295+
var ans []string
296+
if root == nil {
297+
return ans
298+
}
299+
stack := []*TreeNode{root}
300+
node := root
301+
for len(stack) > 0 {
302+
if node == nil {
303+
ans = append(ans, "null")
304+
}
305+
for node != nil {
306+
ans = append(ans, strconv.Itoa(node.Val))
307+
stack = append(stack, node)
308+
node = node.Left
309+
}
310+
node = stack[len(stack)-1].Right
311+
stack = stack[:len(stack)-1]
312+
}
313+
return ans
314+
}

0 commit comments

Comments
 (0)