-
Notifications
You must be signed in to change notification settings - Fork 7
/
BSTNodeDeleter.java
58 lines (46 loc) · 1.96 KB
/
BSTNodeDeleter.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
54
55
56
57
58
package binary_search_trie;
public class BSTNodeDeleter {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
if (root.val > key) {
root.left = deleteNode(root.left, key);
} else if (root.val < key) {
root.right = deleteNode(root.right, key);
} else {
if (root.left == null) return root.right;
if (root.right == null) return root.left;
TreeNode rightSmallest = root.right;
while (rightSmallest.left != null) rightSmallest = rightSmallest.left;
rightSmallest.left = root.left;
return root.right;
}
return root;
}
private static TreeNode constructTree(Integer[] arr, int i) {
if (i >= arr.length || arr[i] == null) return null;
TreeNode root = new TreeNode(arr[i]);
root.left = constructTree(arr, 2 * i + 1);
root.right = constructTree(arr, 2 * i + 2);
return root;
}
public static void main(String[] args) {
BSTNodeDeleter deleter = new BSTNodeDeleter();
TreeNode root1 = constructTree(new Integer[]{5,3,6,2,4,null,7}, 0);
TreeNode expected1 = constructTree(new Integer[]{5,4,6,2,null,null,7}, 0);
// We can only check if the root is the same after deletion, as there can be multiple valid answers
assert deleter.deleteNode(root1, 3).val == expected1.val : "Test case 1 failed";
TreeNode root2 = constructTree(new Integer[]{5,3,6,2,4,null,7}, 0);
TreeNode expected2 = constructTree(new Integer[]{5,3,6,2,4,null,7}, 0);
assert deleter.deleteNode(root2, 0).val == expected2.val : "Test case 2 failed";
assert deleter.deleteNode(null, 0) == null : "Test case 3 failed";
System.out.println("All test cases passed!");
}
}