Closest Binary Search Tree Value
Source
- leetcode: 270
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.
题解
根据二叉树的性质,我们知道当遍历到某个根节点时,最近的那个节点要么是在子树里面,要么就是根节点本身。所以我们根据这个递归,返回子树中最近的节点,和根节点中更近的那个就行了。
java
public int closestValue(TreeNode root, double target) {
// 选出子树的根节点
TreeNode kid = target < root.val ? root.left : root.right;
// 如果没有子树,也就是递归到底时,直接返回当前节点值
if(kid == null) return root.val;
// 找出子树中最近的那个节点
int closest = closestValue(kid, target);
// 返回根节点和子树最近节点中,更近的那个节点
return Math.abs(root.val - target) < Math.abs(closest - target) ? root.val : closest;
}