-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path701-InsertIntoABinarySearchTree.go
More file actions
218 lines (201 loc) · 6.04 KB
/
701-InsertIntoABinarySearchTree.go
File metadata and controls
218 lines (201 loc) · 6.04 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
package main
// 701. Insert into a Binary Search Tree
// You are given the root node of a binary search tree (BST) and a value to insert into the tree.
// Return the root node of the BST after the insertion.
// It is guaranteed that the new value does not exist in the original BST.
// Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion.
// You can return any of them.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg" />
// Input: root = [4,2,7,1,3], val = 5
// Output: [4,2,7,1,3,5]
// Explanation: Another accepted tree is:
// Example 2:
// Input: root = [40,20,60,10,30,50,70], val = 25
// Output: [40,20,60,10,30,50,70,null,null,25]
// Example 3:
// Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
// Output: [4,2,7,1,3,5]
// Constraints:
// The number of nodes in the tree will be in the range [0, 10^4].
// -10^8 <= Node.val <= 10^8
// All the values Node.val are unique.
// -10^8 <= val <= 10^8
// It's guaranteed that val does not exist in the original BST.
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 递归
func insertIntoBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return &TreeNode{Val: val}
}
if val < root.Val {
root.Left = insertIntoBST(root.Left, val)
} else {
root.Right = insertIntoBST(root.Right, val)
}
return root
}
// bfs
func insertIntoBST1(root *TreeNode, val int) *TreeNode {
if root == nil {
return &TreeNode{Val: val}
}
node := root
for {
if node.Val > val {
if node.Left != nil {
node = node.Left
} else {
node.Left = &TreeNode{Val: val}
break
}
}
if node.Val < val {
if node.Right != nil {
node = node.Right
} else {
node.Right = &TreeNode{Val: val}
break
}
}
}
return root
}
// dfs
func insertIntoBST2(root *TreeNode, val int) *TreeNode {
var dfs func (root *TreeNode, val int) *TreeNode
dfs = func (root *TreeNode, val int) *TreeNode {
if root == nil {
return &TreeNode{Val: val}
}
if val < root.Val {
root.Left = dfs(root.Left, val)
} else {
root.Right = dfs(root.Right, val)
}
return root
}
return dfs(root, val)
}
func insertIntoBST3(root *TreeNode, val int) *TreeNode {
p := root
var prev *TreeNode = nil
for p != nil {
prev = p
if p.Val == val {
return root
} else if p.Val > val {
p = p.Left
} else {
p = p.Right
}
}
if prev == nil {
prev = &TreeNode{Val: val}
root = prev
} else if prev.Val > val {
prev.Left = &TreeNode{Val: val}
} else {
prev.Right = &TreeNode{Val: val}
}
return root
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg" />
// Input: root = [4,2,7,1,3], val = 5
// Output: [4,2,7,1,3,5]
// Explanation: Another accepted tree is:
tree1 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil}, &TreeNode{3, nil, nil}, },
&TreeNode{7, nil, nil},
}
fmt.Println(insertIntoBST(tree1, 5))
// Example 2:
// Input: root = [40,20,60,10,30,50,70], val = 25
// Output: [40,20,60,10,30,50,70,null,null,25]
tree2 := &TreeNode {
40,
&TreeNode{20, &TreeNode{10, nil, nil}, &TreeNode{30, nil, nil}, },
&TreeNode{60, &TreeNode{50, nil, nil}, &TreeNode{70, nil, nil}, },
}
fmt.Println(insertIntoBST(tree2, 25))
// Example 3:
// Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
// Output: [4,2,7,1,3,5]
tree3 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil}, &TreeNode{3, nil, nil}, },
&TreeNode{7, nil, nil},
}
fmt.Println(insertIntoBST(tree3, 5))
tree11 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil}, &TreeNode{3, nil, nil}, },
&TreeNode{7, nil, nil},
}
fmt.Println(insertIntoBST1(tree11, 5))
tree12 := &TreeNode {
40,
&TreeNode{20, &TreeNode{10, nil, nil}, &TreeNode{30, nil, nil}, },
&TreeNode{60, &TreeNode{50, nil, nil}, &TreeNode{70, nil, nil}, },
}
fmt.Println(insertIntoBST1(tree12, 25))
tree13 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil}, &TreeNode{3, nil, nil}, },
&TreeNode{7, nil, nil},
}
fmt.Println(insertIntoBST1(tree13, 5))
tree21 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil}, &TreeNode{3, nil, nil}, },
&TreeNode{7, nil, nil},
}
fmt.Println(insertIntoBST2(tree21, 5))
tree22 := &TreeNode {
40,
&TreeNode{20, &TreeNode{10, nil, nil}, &TreeNode{30, nil, nil}, },
&TreeNode{60, &TreeNode{50, nil, nil}, &TreeNode{70, nil, nil}, },
}
fmt.Println(insertIntoBST2(tree22, 25))
tree23 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil}, &TreeNode{3, nil, nil}, },
&TreeNode{7, nil, nil},
}
fmt.Println(insertIntoBST2(tree23, 5))
tree31 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil}, &TreeNode{3, nil, nil}, },
&TreeNode{7, nil, nil},
}
fmt.Println(insertIntoBST3(tree31, 5))
tree32 := &TreeNode {
40,
&TreeNode{20, &TreeNode{10, nil, nil}, &TreeNode{30, nil, nil}, },
&TreeNode{60, &TreeNode{50, nil, nil}, &TreeNode{70, nil, nil}, },
}
fmt.Println(insertIntoBST3(tree32, 25))
tree33 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil}, &TreeNode{3, nil, nil}, },
&TreeNode{7, nil, nil},
}
fmt.Println(insertIntoBST3(tree33, 5))
}