#BST0000008. Xóa nút lá (Delete a Leaf from a BST)
Xóa nút lá (Delete a Leaf from a BST)
Delete a Leaf from a BST
Source: Phuoc Hung OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Build a BST from in the given insertion order. Duplicate keys are ignored.
A key is guaranteed to exist, and its node is guaranteed to be a leaf, meaning that it has neither a left child nor a right child.
Delete the node with key , then print the preorder traversal of the remaining tree. Preorder visits the root, then the left subtree, then the right subtree.
If is the only node, the tree becomes empty after deletion.
Input
The first line contains .
The second line contains signed integers .
The third line contains the key to delete.
Output
If the resulting tree is non-empty, print its preorder keys separated by one space.
If it is empty, print EMPTY.
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
4
Output
8 3 1 6 10 14
Explanation
In the constructed BST, node is the left child of node and has no children, so it is a leaf. Removing it leaves all other links unchanged.
The resulting preorder traversal is 8 3 1 6 10 14.