-
-
Notifications
You must be signed in to change notification settings - Fork 611
/
DeleteNodeinaBST.java
53 lines (48 loc) · 1.9 KB
/
DeleteNodeinaBST.java
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
package problems.medium;
import problems.utils.TreeNode;
/**
* Created by sherxon on 1/12/17.
*/
public class DeleteNodeinaBST {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
if (root.left == null && root.right == null && root.val == key) return null;
root = remove(root, root, key);
return root;
}
TreeNode remove(TreeNode x, TreeNode parent, int key) {
if (x == null) return null; // base case
if (x.val > key)
remove(x.left, x, key); // search from left
else if (x.val < key)
remove(x.right, x, key); // search from right
else { // found, now remove it
if (x.left == null && x.right == null) { // case 1: when the node has no children
if (parent.left == x) parent.left = null;
else parent.right = null;
} else if (x.left != null && x.right != null) { // when the node has two children
TreeNode successor = x.right;
parent = x;
while (successor.left != null) {
parent = successor;
successor = successor.left;
}
int temp = x.val;
x.val = successor.val;
successor.val = temp;
remove(successor, parent, key);
} else { // when the node has only one child
if (parent == x) {
x = parent.left == null ? parent.right : parent.left;
} else if (x.left == null) {
if (parent.left == x) parent.left = x.right;
else parent.right = x.right;
} else {
if (parent.left == x) parent.left = x.left;
else parent.right = x.left;
}
}
}
return x;
}
}