-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteNodeInBST.java
More file actions
54 lines (48 loc) · 1.51 KB
/
DeleteNodeInBST.java
File metadata and controls
54 lines (48 loc) · 1.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
/*
In a BST node deletion, we need to handle three cases:
1.) Delete node has no children
2.) Delete node has 1 child (left || right);
3.) Delete node has 2 children
-Find next in order successor
-Set node to in order successor
-Delete in order successor from tree
*/
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return null;
/* Find Node */
if(key < root.val){
root.left = deleteNode(root.left, key);
} else if(key > root.val){
root.right = deleteNode(root.right, key);
} else {
/* Node Found */
if(root.left == null){ //Handle case #1 & #2
return root.right;
} else if(root.right == null){ //Handle Case #1 & #2
return root.left;
}
// Handle case #3
TreeNode inOrderSuccessor = inOrderSuccessor(root);
root.val = inOrderSuccessor.val;
root.right = deleteNode(root.right, inOrderSuccessor.val);
}
return root;
}
public TreeNode inOrderSuccessor(TreeNode root){
TreeNode candidate = root.right;
while(candidate.left != null){
candidate = candidate.left;
}
return candidate;
}
}