#BST0000009. Xóa nút có một con (Delete a One-Child Node from a BST)
Xóa nút có một con (Delete a One-Child Node from a BST)
Delete a One-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 one child. When deleting , that unique child takes the position of :
- if is not the root, the parent of is connected directly to the unique child;
- if is the root, the unique child becomes the new root.
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 child.
Examples
Input
4
8 3 10 14
10
Output
8 3 14
Explanation
Node is the right child of and has exactly one child, . After deleting , node is connected directly as the right child of .
The resulting preorder traversal is 8 3 14.