Recursive delete
Time O(h) Space O(h)Recurse left or right to find key. At the node: return the non-null child if one is missing; otherwise copy the successor and delete it on the right.
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
if (key < root.val) root.left = deleteNode(root.left, key);
else if (key > root.val) root.right = deleteNode(root.right, key);
else {
if (root.left == null) return root.right;
if (root.right == null) return root.left;
TreeNode s = root.right;
while (s.left != null) s = s.left;
root.val = s.val;
root.right = deleteNode(root.right, s.val);
}
return root;
}
}Verdict: Standard textbook delete.