-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.go
More file actions
239 lines (215 loc) · 5.09 KB
/
Copy pathbinaryTree.go
File metadata and controls
239 lines (215 loc) · 5.09 KB
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
package binaryTree
import (
ut "github.com/CodingPapi/LeetCodeGo/util"
)
// 124
func maxPathSum(root *ut.TreeNode) int {
maxPath := ut.MinInt32
maxPathDfs(root, &maxPath)
return maxPath
}
func maxPathDfs(root *ut.TreeNode, max *int) int {
if root == nil {
return ut.MinInt32
}
maxLeft := maxPathDfs(root.Left, max)
maxRight := maxPathDfs(root.Right, max)
result := ut.MaxInt(root.Val, maxLeft + root.Val)
result = ut.MaxInt(result, maxRight + root.Val)
*max = ut.MaxInt(*max, result)
*max = ut.MaxInt(*max, maxLeft + maxRight + root.Val)
return result
}
// 236
func lowestCommonAncestor(root, p, q *ut.TreeNode) *ut.TreeNode {
if root == nil {
return nil
}
if root.Val == p.Val || root.Val == q.Val {
return root
}
leftFound := lowestCommonAncestor(root.Left, p, q)
rightFound := lowestCommonAncestor(root.Right, p, q)
if leftFound == nil {
return rightFound
}
if rightFound == nil {
return leftFound
}
return root
}
// 102
func levelOrder(root *ut.TreeNode) [][]int {
// bfs
if root == nil {
return [][]int{}
}
result := make([][]int, 0)
leafContainer := make([]*ut.TreeNode, 0)
leafContainer = append(leafContainer, root)
for i := 0; i < len(leafContainer); {
layer := make([]int, 0)
length := len(leafContainer)
for i < length {
layer = append(layer, leafContainer[i].Val)
if leafContainer[i].Left != nil {
leafContainer = append(leafContainer, leafContainer[i].Left)
}
if leafContainer[i].Right != nil {
leafContainer = append(leafContainer, leafContainer[i].Right)
}
i++
}
result = append(result, layer)
}
return result
}
// 107
func levelOrderBottom(root *ut.TreeNode) [][]int {
// dfs
result := make([][]int, 0)
dfsOrderBottom(root, 0, &result)
reverse := make([][]int, 0)
for i := len(result) -1; i >= 0; i-- {
reverse = append(reverse, result[i])
}
return reverse
}
func dfsOrderBottom(root *ut.TreeNode, level int, result *[][]int) {
if root == nil {
return
}
if len(*result) <= level {
*result = append(*result, make([]int,0))
}
(*result)[level] = append((*result)[level], root.Val)
dfsOrderBottom(root.Left, level + 1, result)
dfsOrderBottom(root.Right, level + 1, result)
}
// 103
func zigzagLevelOrder(root *ut.TreeNode) [][]int {
// bfs
type LayerNode struct {
root *ut.TreeNode
next *LayerNode
}
if root == nil {
return [][]int{}
}
result := make([][]int, 0)
layerNode := &LayerNode{root: root, next: nil}
reverse := false
for layerNode != nil {
layerResult := make([]int, 0)
var tempLayer *LayerNode
for layerNode != nil {
first := layerNode.root.Left
second := layerNode.root.Right
if reverse {
first, second = second, first
}
layerResult = append(layerResult, layerNode.root.Val)
if first != nil {
tempLayer = &LayerNode{first, tempLayer}
}
if second != nil {
tempLayer = &LayerNode{second, tempLayer}
}
layerNode = layerNode.next
}
result = append(result, layerResult)
layerNode = tempLayer
reverse = !reverse
}
return result
}
// 98, slow
func isValidBST(root *ut.TreeNode) bool {
if root == nil {
return true
}
maxLeft, validLeft := getMaxOnLeft(root.Left)
minRight, validRight := getMinOnRight(root.Right)
if !validLeft || !validRight {
return false
}
if maxLeft >= root.Val || minRight <= root.Val {
return false
}
deepCheckLeft := isValidBST(root.Left)
deepCheckRight := isValidBST(root.Right)
return deepCheckLeft && deepCheckRight
}
func getMaxOnLeft(root *ut.TreeNode) (int, bool) {
if root == nil {
return ut.MinInt32, true
}
maxLeft, validLeft := getMaxOnLeft(root.Left)
maxRight, validRight := getMaxOnLeft(root.Right)
if !validLeft || !validRight {
return ut.MaxInt(maxLeft, maxRight), false
}
if maxLeft >= root.Val {
return maxLeft, false
}
maxResult := ut.MaxInt(ut.MaxInt(maxLeft, maxRight), root.Val)
return maxResult, true
}
func getMinOnRight(root *ut.TreeNode) (int, bool) {
if root == nil {
return ut.MaxInt32, true
}
minLeft, validLeft := getMinOnRight(root.Left)
minRight, validRight := getMinOnRight(root.Right)
if !validLeft || !validRight {
return ut.MinInt(minLeft, minRight), false
}
if minRight <= root.Val {
return minRight, false
}
minResult := ut.MinInt(ut.MinInt(minLeft, minRight), root.Val)
return minResult, true
}
// 98, fast
func isValidBSTFast(root *ut.TreeNode) bool {
preVal := int(ut.MinInt32)
return checkBSTValue(root, &preVal)
}
func checkBSTValue(root *ut.TreeNode, preVal *int) bool {
if root == nil {
return true
}
if !checkBSTValue(root.Left, preVal) {
return false
}
if root.Val <= *preVal {
return false
}
*preVal = root.Val
return checkBSTValue(root.Right, preVal)
}
// 701
func insertIntoBst(root *ut.TreeNode, val int) *ut.TreeNode {
if root == nil {
return &ut.TreeNode{Val: val}
}
searchBinaryTree(root, val)
return root
}
func searchBinaryTree(root *ut.TreeNode, val int) {
var nextRoot *ut.TreeNode
if root.Val > val {
nextRoot = root.Left
if root.Left == nil {
root.Left = &ut.TreeNode{Val: val}
return
}
} else {
if root.Right == nil {
root.Right = &ut.TreeNode{Val: val}
return
}
nextRoot = root.Right
}
searchBinaryTree(nextRoot, val)
}