Loading...
Loading...
Loading...
Delete a key from a binary search tree. A node with two children is replaced by its inorder successor, the smallest key of its right subtree. If the key is not in the tree, nothing changes. Print the preorder keys of the final tree, or empty if no key is left.
Example 1
Input
8 3 10 1 6 # 14 # # 4 7 13 3
Output
8 4 1 6 7 10 14 13
Example 2
Input
5 5
Output
empty
Try the problem first. Open one hint at a time.
A leaf is removed. A node with one child is replaced by that child.
With two children, copy the smallest key of the right subtree into the node, then delete that key from the right subtree.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor