lintcode900 - Closest Binary Search Tree Value - easy

Posted jasminemzy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
        
    }
}

 














以上是关于lintcode900 - Closest Binary Search Tree Value - easy的主要内容,如果未能解决你的问题,请参考以下文章

lintcode-medium-Subarray Sum Closest

K Closest Points Lintcode

[LintCode] 459 Closest Number in Sorted Array

lintcode459- Closest Number in Sorted Array- easy

5 - Binary Tree & Tree-based DFS

LintCode入门题目