lintcode900 - Closest Binary Search Tree Value - easy

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
Example
Given root = {1}, target = 4.428571, return 1.
Notice
* 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.

 

O(h)。注意不是O(logn),没说平衡。
DFS树的递归。

夹逼法:
注意题目给的一个条件,是BST,所以要利用好这个条件。利用的方法就是分别找和lower bound和upper bound 。
1.lower bound: 比target只小一点点的数。如果根>=target了,那肯定得去左树找;如果根<target,那结果肯定是根或者右树里更好的一个。
2.upper bound: 对称同理。
3.综合,选两bound间更小的那个。

三者擂台法:
对比root,左边王者,右边王者。
这种方法没利用上BST信息进行剪枝,夹逼法每次砍半,这个每次两边还是都得去,遍历全树。但是此方法更general,什么树都可以这样做。

细节:
1.返回TreeNode不返回值,这样正好可以用返回null标记遇到null。

 

1.夹逼法

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: the given BST
     * @param target: the given target
     * @return: the value in the BST that is closest to the target
     */
    public int closestValue(TreeNode root, double target) {
        // write your code here
        
        TreeNode lb = lowerBound(root, target);
        TreeNode ub = upperBound(root, target);
        int result = -1;
        double closestDist = Double.MAX_VALUE;
        
        if (lb != null && (target - lb.val) < closestDist) {
            closestDist = target - lb.val;
            result = lb.val;
        }
        if (ub != null && (ub.val - target) < closestDist) {
            closestDist = ub.val - target;
            result = ub.val;
        }
        return result;
    }
    
    private TreeNode lowerBound(TreeNode root, double target) {
        if (root == null) {
            return null;
        }
        
        if (root.val > target) {
            return lowerBound(root.left, target);
        }
        TreeNode right = lowerBound(root.right, target);
        if (right != null) {
            return right;
        }
        return root;
    }
    
    private TreeNode upperBound(TreeNode root, double target) {
        if (root == null) {
            return null;
        }
        if (root.val < target) {
            return upperBound(root.right, target);
        }
        TreeNode left = upperBound(root.left, target);
        if (left != null) {
            return left;
        }
        return root;
    }
    
    
}

 

2.三者擂台法

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: the given BST
     * @param target: the given target
     * @return: the value in the BST that is closest to the target
     */
    public int closestValue(TreeNode root, double target) {
        // write your code here
        if (root== null) {
            return -1;
        } 
        
        double minDist = Math.abs(target - root.val);
        int minVal = root.val;
        
        if (root.left != null) {
            int leftClosest = closestValue(root.left, target);
            if (Math.abs(target - leftClosest) < minDist) {
                minDist = Math.abs(target - leftClosest);
                minVal = leftClosest;
            }
        } 
        if (root.right != null) {
            int rightClosest = closestValue(root.right, target);
            if (Math.abs(target - rightClosest) < minDist) {
                minDist = Math.abs(target - rightClosest);
                minVal = rightClosest;
            }
        } 
        return minVal;
        
    }
}

 

转载于:https://www.cnblogs.com/jasminemzy/p/9565269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值