题目:

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.

链接: http://leetcode.com/problems/closest-binary-search-tree-value/

题解:

求BST中跟target最近的数字。我们先设置一个min = root.val,然后用iterative的办法尝试更新min, 然后比较target与root的大小,进行二分查找。

Time Complexity - O(logn), Space Complexity - O(1)。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int closestValue(TreeNode root, double target) {
int min = root.val;
while(root != null) {
min = Math.abs(target - root.val) < Math.abs(target - min) ? root.val : min;
root = root.val < target ? root.right : root.left;
} return min;
}
}

二刷:

这道题也是主要考察binary search。方法和一刷一样。  可以有递归和迭代。

Java:

迭代

Time Complexity - O(logn), Space Complexity - O(1)。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int closestValue(TreeNode root, double target) {
int min = root.val;
while (root != null) {
min = Math.abs(root.val - target) < Math.abs(min -target) ? root.val : min;
root = target < root.val ? root.left : root.right;
}
return min;
}
}

递归:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int closestValue(TreeNode root, double target) {
TreeNode child = target < root.val ? root.left : root.right;
if (child == null) {
return root.val;
}
int childClosest = closestValue(child, target);
return Math.abs(root.val - target) < Math.abs(childClosest - target) ? root.val : childClosest;
}
}

三刷:

Java:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int closestValue(TreeNode root, double target) {
if (root == null) return 0;
int min = root.val;
while (root != null) {
min = (Math.abs(root.val - target) < Math.abs(min - target) ? root.val : min);
root = (root.val < target) ? root.right : root.left;
}
return min;
}
}

Reference:

https://leetcode.com/discuss/54438/4-7-lines-recursive-iterative-ruby-c-java-python

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

  1. 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 clo ...

  2. 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 t ...

  3. [leetcode]270. Closest Binary Search Tree Value二叉搜索树中找target的最接近值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  4. [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 t ...

  5. [LC] 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 t ...

  6. 【LeetCode】270. Closest Binary Search Tree Value 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  7. [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  8. [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 t ...

  9. [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 t ...

随机推荐

  1. 软件工程随堂小作业—— 寻找“水王”(C++)

    一.设计思路 (1)输入发帖ID记录表 (2)从第一个ID开始,与后续的发帖ID进行比较,若相同计数器则加一,否则减一.若计数器的数值被减为零,则重新选取当前ID开始记录比较. (3)输出结果 二.源 ...

  2. using详解(C#)

    using肯定所有人都用过,最简单的就是使用using引入命名空间,然后就是引入别名,简化输入,本文主要介绍第三种用法,即用using强制对象清理资源. 先看下面这段代码: try { using ( ...

  3. Upgrading to Java 8——第一章 Lambda表达式

    第一章 Lambda表达式 Lamada 表达式是Java SE 8中最重要的新特性,长期以来被认为是在Java中缺失的特性,它的出现使整个java 语言变得完整.至少到目前,在这节中你将学习到什么是 ...

  4. PP生产订单的BADI增强 WORKORDER_UPDATE

    METHOD if_ex_workorder_update~before_update. *---------------------->增强1 开始* "当生产订单类型为PP01时, ...

  5. RobotFramework-调用.py文件

    RobotFramework-调用.py文件,直接运行: 注意:文件路径的\全部换成好了/

  6. 【VS2012】项目文件夹管理

    项目中添加文件夹 " 项目"显示所有文件 在"显示所有文件"的情况下,可以创建文件件 "新建文件夹"需要添加到物理路径中时,可以选择&quo ...

  7. 使用Npoi向Excel中插入图片

    先把数据库中的数据都导入到Excel表格中,把图片地址的路径全部转成绝对路径. 使用Npoi读取刚导出的Excle表格,把图片那个单元格的图片路径读出来,然后用文件流读取图片,然后通过Npoi把图片放 ...

  8. c++取小数整数部分

    #include<math.h> double ceil(double x) //向上取整 double floor(double x) //向下取整 向上取整,取比x大的第一个整数值向下 ...

  9. shader 里面的分支

    shader 里面的真分支会降低效率 一种方法:构造一个分段函数出来 比如saturate(depth*1.5f)

  10. color mask

    https://msdn.microsoft.com/zh-cn/library/windows/desktop/bb173595(v=vs.85).aspx void OMSetBlendState ...