#BST0000007. Predecessor trong BST (Inorder Predecessor in a BST)
Predecessor trong BST (Inorder Predecessor in a BST)
Inorder Predecessor in a BST
Source: Phuoc Hung OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Build a BST from in insertion order. Duplicate insertion keys are ignored.
A key is guaranteed to exist in the tree. The strict inorder predecessor of is the largest stored key that is smaller than .
Find this strict inorder predecessor. If no stored key is smaller than , the predecessor does not exist.
Input
The first line contains .
The second line contains signed integers in insertion order.
The third line contains .
Output
Print the predecessor key if it exists; otherwise print NONE.
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 is guaranteed to belong to the resulting BST.
Examples
Input
7
8 3 10 1 6 14 4
6
Output
4
Explanation
The stored keys smaller than are . The largest of them is , so the strict inorder predecessor of is .