Delete from BST
Delete a given value from a Binary Search Tree and print the resulting tree. When the deleted node has two children, replace it with its in-order successor (the smallest value in its right subtree).
The result is printed in canonical level-order form (breadth-first, null for a real node's missing child, trailing null tokens removed).
Input Format
The first line contains an integer M. The second line contains M space-separated tokens (integers or null) — the BST in level order. The third line contains the integer X to delete (it is present in the tree).
Output Format
Print the canonical level-order serialization of the BST after the deletion (an empty line if the tree becomes empty).
7 5 3 7 2 4 6 8 3
5 4 7 2 null 6 8
Explanation: Node 3 has two children, so it is replaced by its in-order successor 4.
- 1 <= number of nodes <= 5000
- All node values are distinct.
- X is present in the tree.
- -1000000000 <= value, X <= 1000000000