-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathTreeNode.go
73 lines (64 loc) · 1.33 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
package Solution
import (
"fmt"
"strconv"
)
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func GenTree(x []int) *TreeNode {
index := 0
return GenTreeHelp(x, &index)
}
func GenTreeHelp(x []int, index *int) *TreeNode {
if len(x) == *index {
return nil
}
fmt.Println(x, x[*index])
if x[*index] == -1 {
*index = *index + 1
return nil
}
// fmt.Print(*index, x[*index])
(*index) = *index + 1
node := &TreeNode{Val: x[*index]}
node.Left = GenTreeHelp(x, index)
return node
}
// 序列化
func dumpTreeToString(tree *TreeNode) string {
var str string = ""
dumpTreeToStringHelper(tree, &str)
return str
}
func dumpTreeToStringHelper(tree *TreeNode, str *string) {
if tree == nil {
*str += "#"
return
}
*str += strconv.Itoa(tree.Val)
dumpTreeToStringHelper(tree.Left, str)
dumpTreeToStringHelper(tree.Right, str)
}
// 反序列化
func conFromPreStr(str string) *TreeNode {
var strIndex int = 0
return conFromPreStrHelper(str, &strIndex)
}
func conFromPreStrHelper(str string, index *int) *TreeNode {
if len(str) == *index {
return nil
}
if (str)[*index] == '#' {
(*index) = *index + 1
return nil
}
v, _ := strconv.Atoi(string((str)[*index]))
root := &TreeNode{Val: v}
(*index) = *index + 1
root.Left = conFromPreStrHelper(str, index)
root.Right = conFromPreStrHelper(str, index)
return root
}