-
Notifications
You must be signed in to change notification settings - Fork 1
/
114_flatten_test.go
52 lines (44 loc) · 949 Bytes
/
114_flatten_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package _5_binary_tree
import (
"github.com/smartystreets/goconvey/convey"
"testing"
)
// 二叉树展开为链表 https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
func TestFlatten(t *testing.T) {
convey.Convey(" 二叉树展开为链表", t, func() {
testCase := []struct {
input *TreeNode
}{
{
Ints2TreeNode([]int{1, 2, 5, 3, 4, NULL, 6}),
},
{
Ints2TreeNode([]int{}),
},
{
Ints2TreeNode([]int{0}),
},
}
for _, tst := range testCase {
flatten(tst.input)
}
})
}
// 方式一:递归
func flatten(root *TreeNode) {
var ans = make([]*TreeNode, 0)
var dfs func(node *TreeNode)
dfs = func(node *TreeNode) {
if node == nil {
return
}
ans = append(ans, node)
dfs(node.Left)
dfs(node.Right)
}
dfs(root)
for i := 1; i < len(ans); i++ {
prev, curr := ans[i-1], ans[i]
prev.Left, prev.Right = nil, curr
}
}