#BST0000001. Đường tìm kiếm trong BST (BST Search Path)
Đường tìm kiếm trong BST (BST Search Path)
BST Search Path
Source: Phuoc Hung OJ
Version: Phuoc Hung OJ Extended
Problem Statement
A binary search tree (BST) is a binary tree in which, for every node with key , every key in its left subtree is smaller than and every key in its right subtree is greater than .
Initially the tree is empty. Insert in the given order. To insert , start at the root: go to the left child if is smaller than the current key, and to the right child if it is larger. Create a new node when the required child is empty. If the key already exists, ignore that insertion and leave the tree unchanged.
After the tree is built, search for the query key . The search starts at the root and follows the same comparison rule. Print all keys of the visited nodes in order, from the root to the point where the search stops.
If a node with key is reached, the final status is FOUND. If the search reaches an empty branch without finding , the final status is NOT FOUND.
Input
The first line contains an integer — the number of keys used to build the tree.
If , the next line contains signed integers in insertion order. If , this line is absent.
The last line contains the query key .
Output
If the tree is non-empty, print the visited keys separated by one space, followed by FOUND or NOT FOUND on the same line.
If the tree is empty, no node is visited; print only:
NOT FOUND
Subtasks
Subtask 1 (20 points): .
Subtask 2 (30 points): .
Subtask 3 (50 points): .
In all subtasks, and are signed 64-bit integers, so . Duplicate insertion keys are ignored.
Examples
Input
7
8 3 10 1 6 14 4
6
Output
8 3 6 FOUND
Explanation
The first key becomes the root. To search for , first visit . Since , move left to . Since , move right and reach .
The visited keys are , and the query key is found, so the final status is FOUND.