-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path450-DeleteNodeInABST.go
More file actions
172 lines (158 loc) · 5.51 KB
/
450-DeleteNodeInABST.go
File metadata and controls
172 lines (158 loc) · 5.51 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
package main
// 450. Delete Node in a BST
// Given a root node reference of a BST and a key, delete the node with the given key in the BST.
// Return the root node reference (possibly updated) of the BST.
// Basically, the deletion can be divided into two stages:
// Search for a node to remove.
// If the node is found, delete the node.
// Example 1:
// 5 5
// / \ / \
// 3 6 => 4 6
// / \ \ / \
// 2 4 8 2 8
// <img src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" />
// Input: root = [5,3,6,2,4,null,7], key = 3
// Output: [5,4,6,2,null,null,7]
// Explanation: Given key to delete is 3. So we find the node with value 3 and delete it.
// One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
// Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
// Example 2:
// Input: root = [5,3,6,2,4,null,7], key = 0
// Output: [5,3,6,2,4,null,7]
// Explanation: The tree does not contain a node with value = 0.
// Example 3:
// Input: root = [], key = 0
// Output: []
// Constraints:
// The number of nodes in the tree is in the range [0, 10^4].
// -10^5 <= Node.val <= 10^5
// Each node has a unique value.
// root is a valid binary search tree.
// -10^5 <= key <= 10^5
// Follow up: Could you solve it with time complexity O(height of tree)?
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 deleteNode(root *TreeNode, key int) *TreeNode {
var dfs func(*TreeNode) *TreeNode
dfs = func(node *TreeNode) *TreeNode {
if node == nil { return nil }
if node.Val == key {
if node.Left == nil { return node.Right }
if node.Right == nil { return node.Left }
curr := node.Right
for curr.Left != nil {
curr = curr.Left
}
curr.Left = node.Left // 移除节点
return node.Right
}
if key > node.Val { // 二分找到要删除的节点
node.Right = dfs(node.Right)
} else {
node.Left = dfs(node.Left)
}
return node
}
return dfs(root)
}
func deleteNode1(root *TreeNode, key int) *TreeNode {
var dfs func (cur **TreeNode,key int)
dfs = func (cur **TreeNode,key int) {
if (*cur) == nil { return }
if key < (*cur).Val {
dfs(&((*cur).Left),key)
} else if key == (*cur).Val {
if (*cur).Left == nil && (*cur).Right == nil {
(*cur) = nil
} else if (*cur).Left == nil && (*cur).Right != nil {
(*cur) = (*cur).Right
} else if (*cur).Left != nil && (*cur).Right == nil {
(*cur) = (*cur).Left
} else {
tmp := (*cur).Right
for tmp.Left != nil{
tmp = tmp.Left
}
(*cur).Val = tmp.Val
dfs(&((*cur).Right),tmp.Val)
}
} else {
dfs(&((*cur).Right),key)
}
}
dfs(&root,key)
return root
}
func main() {
// Example 1:
// 5 5
// / \ / \
// 3 6 => 4 6
// / \ \ / \
// 2 4 7 2 7
// <img src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" />
// Input: root = [5,3,6,2,4,null,7], key = 3
// Output: [5,4,6,2,null,null,7]
// Explanation: Given key to delete is 3. So we find the node with value 3 and delete it.
// One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
// Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
tree1 := &TreeNode {
5,
&TreeNode{3, &TreeNode{2, nil, nil}, &TreeNode{4, nil, nil}, },
&TreeNode{6, nil, &TreeNode{7, nil, nil}, },
}
fmt.Println("tree1.Left: ", tree1.Left)
t1 := deleteNode(tree1,3)
fmt.Println("t1: ", t1) // t1: &{5 0xc000008078 0xc000008090}
fmt.Println("t1.Left: ", t1.Left)
// Example 2:
// Input: root = [5,3,6,2,4,null,7], key = 0
// Output: [5,3,6,2,4,null,7]
// Explanation: The tree does not contain a node with value = 0.
tree2 := &TreeNode {
5,
&TreeNode{3, &TreeNode{2, nil, nil}, &TreeNode{4, nil, nil}, },
&TreeNode{6, nil, &TreeNode{7, nil, nil}, },
}
fmt.Println("tree2: ", tree2)
t2 := deleteNode(tree2, 0)
fmt.Println("t2: ", t2)
// Example 3:
// Input: root = [], key = 0
// Output: []
t3 := deleteNode(nil, 0)
fmt.Println("t3: ", t3)
tree11 := &TreeNode {
5,
&TreeNode{3, &TreeNode{2, nil, nil}, &TreeNode{4, nil, nil}, },
&TreeNode{6, nil, &TreeNode{7, nil, nil}, },
}
fmt.Println("tree11.Left: ", tree11.Left)
t11 := deleteNode1(tree11,3)
fmt.Println("t11: ", t11) // t11: &{5 0xc000008078 0xc000008090}
fmt.Println("t11.Left: ", t11.Left)
tree12 := &TreeNode {
5,
&TreeNode{3, &TreeNode{2, nil, nil}, &TreeNode{4, nil, nil}, },
&TreeNode{6, nil, &TreeNode{7, nil, nil}, },
}
fmt.Println("tree12: ", tree12)
t12 := deleteNode1(tree12, 0)
fmt.Println("t12: ", t12)
t13 := deleteNode1(nil, 0)
fmt.Println("t13: ", t13)
}