Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
Note
- Given target value is a floating point.
- You are guaranteed to have only one unique value in the BST that is closest to the target.
Example
No.1
Input: root = {5,4,9,2,#,8,10} and target = 6.124780
Output: 5
No.2
Input: root = {3,2,4,1} and target = 4.142857
Output: 4
Code
1 | public class TreeNode { |
1 | public int closestValue(TreeNode root, double target) { |