#BST0000010. Xóa nút có hai con (Delete a Two-Child Node from a BST)
Xóa nút có hai con (Delete a Two-Child Node from a BST)
Delete a Two-Child Node from a BST
Source: Phuoc Hung OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Build a BST from in insertion order. Duplicate keys are ignored.
A key is guaranteed to exist, and its node has exactly two children. To make the deletion result unique, you must use the inorder successor of , defined as the node with the smallest key in the right subtree of .
Replace the key at the position of by the successor key, then delete the old successor node from its original position. The resulting tree must remain a BST.
Print the preorder traversal after the deletion.
Input
The first line contains .
The second line contains signed integers .
The third line contains the key to delete.
Output
Print the preorder traversal of the resulting tree on one line, with keys separated by one space.
Subtasks
Subtask 1 (20 points): .
Subtask 2 (30 points): .
Subtask 3 (50 points): .
In all subtasks, and are signed 64-bit integers, duplicate insertion keys are ignored, and the node with key exists and has exactly children.
Examples
Input
7
8 3 10 1 6 14 4
3
Output
8 4 1 6 10 14
Explanation
Node has two children. Its right subtree contains keys and , whose minimum is , so is the inorder successor.
Replace by and remove the old node . The resulting preorder is 8 4 1 6 10 14.