#BST0000024. Dựng cây (Tree Construction)
Dựng cây (Tree Construction)
Tree Construction
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A sequence of distinct integers is used to construct a binary search tree in the following order.
Key becomes the root.
For every from to , start at the root. If is smaller than the current key, move to the left child; if it is larger, move to the right child. When the required child is empty, create a new node with key there and finish this insertion.
For every inserted element from the second one onward, determine the key stored in the direct parent of the newly created node.
Input
The first line contains .
The second line contains distinct integers in insertion order.
Output
Print integers on one line. For every , the -th output integer is the key stored in the parent of the node with key .
Separate consecutive integers by one space.
Subtasks
Subtask 1 (20 points): .
Subtask 2 (30 points): .
Subtask 3 (50 points): , .
In all subtasks, all are distinct.
Examples
Input
5
4 2 3 1 6
Output
4 2 2 4
Explanation
Key is the root. Inserting makes it the left child of , so its parent is .
Next, goes left from to and becomes the right child of , so its parent is . Key becomes the left child of , and key becomes the right child of .
Therefore the parent keys for are 4 2 2 4.