Delete Node in a BST LeetCode solution in Java

1
class Solution {
2
public TreeNode deleteNode(TreeNode root, int key) {
3
if (root == null) return null;
4

5
if (key < root.val) {
6
root.left = deleteNode(root.left, key);
7
return root;
8
} else if (key > root.val) {
9
root.right = deleteNode(root.right, key);
10
return root;
11
} else {
12
if (root.left == null) {
13
return root.right;
14
} else if (root.right == null) {
15
return root.left;
16
} else {
17
TreeNode min = root.right;
18
while (min.left != null) {
19
min = min.left;
20
}
21

22
root.val = min.val;
23
root.right = deleteNode(root.right, min.val);
24
return root;
25
}
26
}
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0