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.

Recursive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/

public class Solution {
public int closestValue(TreeNode root, double target) {
int cur = root.val;
TreeNode child = target < cur ? root.left : root.right;
if (child == null) return cur;
int next = closestValue(child, target);
return Math.abs(target - cur) < Math.abs(target - next) ? cur : next;

}
}

Iterative

1
2
3
4
5
6
7
8
9
10
11
public class Solution {
public int closestValue(TreeNode root, double target) {
int res = root.val;
while (root != null){
if (Math.abs(res - target) >= Math.abs(root.val - target))
res = root.val;
root = target < root.val ? root.left : root.right;
}
return res;
}
}