题目
https://leetcode-cn.com/problems/closest-binary-search-tree-value/
二分查找
根据root值选选择在左子树中查找还是在右子树中查找
class Solution {
public int closestValue(TreeNode root, double target) {
int closest = root.val;
while (root != null) {
int val = root.val;
closest = Math.abs(val - target) < Math.abs(closest - target) ? val : closest;
root = target < val ? root.left : root.right;
}
return closest;
}
}