forked from aQuaYi/LeetCode-in-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.go
219 lines (171 loc) · 3.81 KB
/
TreeNode.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package kit
import (
"fmt"
)
// TreeNode is tree's node
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
// NULL 方便添加测试数据
var NULL = -1 << 63
// Ints2TreeNode 利用 []int 生成 *TreeNode
func Ints2TreeNode(ints []int) *TreeNode {
n := len(ints)
if n == 0 {
return nil
}
root := &TreeNode{
Val: ints[0],
}
queue := make([]*TreeNode, 1, n*2)
queue[0] = root
i := 1
for i < n {
node := queue[0]
queue = queue[1:]
if i < n && ints[i] != NULL {
node.Left = &TreeNode{Val: ints[i]}
queue = append(queue, node.Left)
}
i++
if i < n && ints[i] != NULL {
node.Right = &TreeNode{Val: ints[i]}
queue = append(queue, node.Right)
}
i++
}
return root
}
// GetTargetNode 返回 Val = target 的 TreeNode
// root 中一定有 node.Val = target
func GetTargetNode(root *TreeNode, target int) *TreeNode {
if root == nil || root.Val == target {
return root
}
res := GetTargetNode(root.Left, target)
if res != nil {
return res
}
return GetTargetNode(root.Right, target)
}
func indexOf(val int, nums []int) int {
for i, v := range nums {
if v == val {
return i
}
}
msg := fmt.Sprintf("%d 不存在于 %v 中", val, nums)
panic(msg)
}
// PreIn2Tree 把 preorder 和 inorder 切片转换成 二叉树
func PreIn2Tree(pre, in []int) *TreeNode {
if len(pre) != len(in) {
panic("preIn2Tree 中两个切片的长度不相等")
}
if len(in) == 0 {
return nil
}
res := &TreeNode{
Val: pre[0],
}
if len(in) == 1 {
return res
}
idx := indexOf(res.Val, in)
res.Left = PreIn2Tree(pre[1:idx+1], in[:idx])
res.Right = PreIn2Tree(pre[idx+1:], in[idx+1:])
return res
}
// InPost2Tree 把 inorder 和 postorder 切片转换成 二叉树
func InPost2Tree(in, post []int) *TreeNode {
if len(post) != len(in) {
panic("inPost2Tree 中两个切片的长度不相等")
}
if len(in) == 0 {
return nil
}
res := &TreeNode{
Val: post[len(post)-1],
}
if len(in) == 1 {
return res
}
idx := indexOf(res.Val, in)
res.Left = InPost2Tree(in[:idx], post[:idx])
res.Right = InPost2Tree(in[idx+1:], post[idx:len(post)-1])
return res
}
// Tree2Preorder 把 二叉树 转换成 preorder 的切片
func Tree2Preorder(root *TreeNode) []int {
if root == nil {
return nil
}
if root.Left == nil && root.Right == nil {
return []int{root.Val}
}
res := []int{root.Val}
res = append(res, Tree2Preorder(root.Left)...)
res = append(res, Tree2Preorder(root.Right)...)
return res
}
// Tree2Inorder 把 二叉树转换成 inorder 的切片
func Tree2Inorder(root *TreeNode) []int {
if root == nil {
return nil
}
if root.Left == nil && root.Right == nil {
return []int{root.Val}
}
res := Tree2Inorder(root.Left)
res = append(res, root.Val)
res = append(res, Tree2Inorder(root.Right)...)
return res
}
// Tree2Postorder 把 二叉树 转换成 postorder 的切片
func Tree2Postorder(root *TreeNode) []int {
if root == nil {
return nil
}
if root.Left == nil && root.Right == nil {
return []int{root.Val}
}
res := Tree2Postorder(root.Left)
res = append(res, Tree2Postorder(root.Right)...)
res = append(res, root.Val)
return res
}
// Equal return ture if tn == a
func (tn *TreeNode) Equal(a *TreeNode) bool {
if tn == nil && a == nil {
return true
}
if tn == nil || a == nil || tn.Val != a.Val {
return false
}
return tn.Left.Equal(a.Left) && tn.Right.Equal(a.Right)
}
// Tree2ints 把 *TreeNode 按照行还原成 []int
func Tree2ints(tn *TreeNode) []int {
res := make([]int, 0, 1024)
queue := []*TreeNode{tn}
for len(queue) > 0 {
size := len(queue)
for i := 0; i < size; i++ {
nd := queue[i]
if nd == nil {
res = append(res, NULL)
} else {
res = append(res, nd.Val)
queue = append(queue, nd.Left, nd.Right)
}
}
queue = queue[size:]
}
i := len(res)
for i > 0 && res[i-1] == NULL {
i--
}
return res[:i]
}