-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob0099.go
47 lines (38 loc) · 891 Bytes
/
prob0099.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
package prob0099
import "sort"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
type SortInt []int
func (a SortInt) Len() int { return len(a) }
func (a SortInt) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortInt) Less(i, j int) bool { return a[i] < a[j] }
func recoverTree(root *TreeNode) {
// 先中序排序
order := make([]int, 0)
inorder(root, &order)
// 对内容进行排序
sort.Sort(SortInt(order))
// 再中序排序插入
index := 0
orderInsert(root, &order, &index)
}
func inorder(root *TreeNode, l *[]int) {
if root == nil {
return
}
inorder(root.Left, l)
*l = append(*l, root.Val)
inorder(root.Right, l)
}
func orderInsert(root *TreeNode, l *[]int, index *int) {
if root == nil {
return
}
orderInsert(root.Left, l, index)
root.Val = (*l)[*index]
*index = *index + 1
orderInsert(root.Right, l, index)
}