#BST0000006. Successor trong BST (Inorder Successor in a BST)
Successor trong BST (Inorder Successor in a BST)
Inorder Successor 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 successor of is the smallest stored key that is greater than .
Find this strict inorder successor. If no stored key is greater than , the successor does not exist.
Input
The first line contains .
The second line contains signed integers in insertion order.
The third line contains .
Output
Print the successor 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
8
Explanation
The stored keys greater than are . The smallest of them is , so the strict inorder successor of is .