1. 程式人生 > >lintcode900 - Closest Binary Search Tree Value - easy

lintcode900 - Closest Binary Search Tree Value - easy

close eno right not and 綜合 tco write oot

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