-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree.go
222 lines (184 loc) · 3.36 KB
/
binary_tree.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
package algorithm
import "fmt"
type TNode struct {
Value int
leftNode *TNode
rightNode *TNode
}
func NewTNode(data int) *TNode {
return &TNode{
Value: data,
leftNode: nil,
rightNode: nil,
}
}
func (n *TNode) insert(data int) {
if data < n.Value {
if n.leftNode != nil {
n.leftNode.insert(data)
} else {
n.leftNode = NewTNode(data)
}
} else {
if n.rightNode != nil {
n.rightNode.insert(data)
} else {
n.rightNode = NewTNode(data)
}
}
}
func (n *TNode) middleShow() {
if n.leftNode != nil {
n.leftNode.middleShow()
}
fmt.Println(n.Value)
if n.rightNode != nil {
n.rightNode.middleShow()
}
}
func (n *TNode) firstShow() {
fmt.Println(n.Value)
if n.leftNode != nil {
n.leftNode.firstShow()
}
if n.rightNode != nil {
n.rightNode.firstShow()
}
}
func (n *TNode) search(data int) *TNode {
if n.Value == data {
return n
} else if data < n.Value {
if n.leftNode != nil {
return n.leftNode.search(data)
}
} else {
if n.rightNode != nil {
return n.rightNode.search(data)
}
}
return nil
}
func (n *TNode) reverse() {
leftNode := n.leftNode
n.leftNode = n.rightNode
n.rightNode = leftNode
if n.leftNode != nil {
n.leftNode.reverse()
}
if n.rightNode != nil {
n.rightNode.reverse()
}
}
// =======================================
type BinaryTree struct {
len int
root *TNode
}
func NewBinaryTree() *BinaryTree {
return &BinaryTree{}
}
func (b *BinaryTree) Insert(data int) {
b.len++
if b.root == nil {
b.root = NewTNode(data)
} else {
b.root.insert(data)
}
}
func (b *BinaryTree) Search(data int) *TNode {
if b.root != nil {
return b.root.search(data)
}
return nil
}
/*
中序
1、递归实现
2、非递归实现,借助栈保存右节点
*/
func (b *BinaryTree) MiddleShow() {
if b.root != nil {
b.root.middleShow()
}
}
// 先序
func (b *BinaryTree) FirstShow() {
if b.root != nil {
b.root.firstShow()
}
}
/*
逐层遍历
用一个队列保存节点,先进先出
*/
func (b *BinaryTree) LevelShow() {
queue := make([]*TNode, 0)
result := make([]int, 0)
queue = append(queue, b.root)
for len(queue) > 0 {
result = append(result, queue[0].Value)
if queue[0].leftNode != nil {
queue = append(queue, queue[0].leftNode)
}
if queue[0].rightNode != nil {
queue = append(queue, queue[0].rightNode)
}
queue = queue[1:]
}
fmt.Println(result)
}
// 翻转二叉树
func (b *BinaryTree) Reverse() {
if b.root != nil {
b.root.reverse()
}
}
/*
参考
- https://learnku.com/articles/48638
*/
/*
反转二叉树
https://leetcode-cn.com/problems/invert-binary-tree/
*/
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left, root.Right = root.Right, root.Left
invertTree(root.Right)
invertTree(root.Left)
return root
}
/*
501. 二叉搜索树中的众数
https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/
思路:
1、深度遍历,存map
2、遍历map
*/
func findMode(root *TreeNode) []int {
m := make(map[int]int)
deepSearch(root, m)
moreList := make([]int, 0)
moreNum := 0
for n, num :=range m {
if num > moreNum {
moreNum = num
moreList = make([]int, 0)
moreList = append(moreList, n)
}else if num == moreNum {
moreList = append(moreList, n)
}
}
return moreList
}
func deepSearch(n *TreeNode, m map[int]int){
if n == nil {
return
}
m[n.Val] ++
deepSearch(n.Left, m)
deepSearch(n.Right, m)
}