Skip to content
C

Delete from BST

Mediumjavapythonccppjavascript

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).

Input (stdin)

Output

Run your code to see output here...