270. Closest Binary Search Tree Value

        /*
     * 270. Closest Binary Search Tree Value
     * 2016-6-25 by Mingyang
     */
      public int closestValue(TreeNode root, double target) {
            int closest = root.val;
            double min = Double.MAX_VALUE;
            while(root!=null) {
                if( Math.abs(root.val - target) < min  ) {
                    min = Math.abs(root.val - target);
                    closest = root.val;
                }
                if(target < root.val) {
                    root = root.left;
                } else if(target > root.val) {
                    root = root.right;
                } else {
                    return root.val;
                }
            }
            return closest;
        }
时间: 2024-08-08 01:18:53

270. Closest Binary Search Tree Value的相关文章

Leetcode 270. Closest Binary Search Tree Value

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.

270. 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. 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.

LeetCode Closest Binary Search Tree Value

原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ 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

272. Closest Binary Search Tree Value II

/* * 272. Closest Binary Search Tree Value II * 2016-6-25 by Mingyang * 一开始这个题目我想用priority queue来做,就是把所有的stack里面的全部加进来 * 然后一个一个的找 */ public List<Integer> closestKValues1(TreeNode root, double target, int k) { Stack<TreeNode> stack = new Stack&

[LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, that is: k ≤ total nodes. You are guaranteed to have onl

Closest Binary Search Tree Value -- LeetCode

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.

*Closest Binary Search Tree Value

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. public class Solution { public int closestValue(TreeNode root, double target) { int closestVal = root.val; while(root!=null) { closestVa

[?]*Closest Binary Search Tree Value II

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, that is: k ≤ total nodes. You are guaranteed to have onl

[LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

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.