#K51222. Closest Value in a Binary Search Tree
Closest Value in a Binary Search Tree
Closest Value in a Binary Search Tree
You are given a binary search tree (BST) and a target value \(T\). Your task is to find the value in the BST that is closest to \(T\). In a BST, for any node with value \(V\), all values in its left subtree are less than \(V\) and all values in its right subtree are greater than \(V\). The target \(T\) is a floating-point number and the answer should be an integer from the tree that minimizes \(|V - T|\).
Example:
Input: 5 4 2 5 1 3 3.714286 Output: 4
Explanation: The BST is constructed by inserting the nodes in the given order. For the target 3.714286, the closest value in the tree is 4.
inputFormat
The input is given via standard input (stdin) in the following format:
- An integer \(N\) representing the number of nodes in the BST.
- A line with \(N\) space-separated integers representing the nodes in the order they are inserted into the BST.
- A floating-point number representing the target value \(T\).
Note: It is guaranteed that the given sequence of integers forms a valid BST when inserted sequentially.
outputFormat
Output a single integer, which is the value in the BST that is closest to the target value \(T\). The result should be printed to standard output (stdout).
## sample5
4 2 5 1 3
3.714286
4
</p>