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