-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathTreeNode.go
314 lines (262 loc) · 5.9 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package structures
import (
"fmt"
"strconv"
)
// 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]
}
// 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)
// fmt.Printf("****head = %v\n", head.Val)
*array = append(*array, head.Val)
if head.Left != nil {
T2s(head.Left, array)
}
if head.Right != nil {
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
}