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.

Example:

Input: root = [4,2,5,1,3], target = 3.714286

    4
/ \
2 5
/ \
1 3 Output: 4

题目

给定一棵二叉搜索树和一个target,求出其节点中最接近该target的值。

思路

1. based on the BST's attribution ( left < root < right), we use binary search

代码

 class Solution {
public int closestValue(TreeNode root, double target) {
int result = root.val;
while(root != null){
//update result if the current value is closer to target
if(Math.abs(target - root.val) < Math.abs(target - result)){
result = root.val;
}
//binary search
root = root.val > target? root.left: root.right;
}
return result;
}
}

[leetcode]270. Closest Binary Search Tree Value二叉搜索树中找target的最接近值的更多相关文章

  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. [LC] 700题 Search in a Binary Search Tree (二叉搜索树中的搜索) (二叉搜索树)

    ①中文题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 在上述示例 ...

  3. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

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

  6. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

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

随机推荐

  1. The Google File System 中文版

    摘要 我们设计并实现了Google文件系统,一个面向分布式数据密集型应用的.可伸缩的分布式文件系统.虽然运行在廉价的日用硬件设备上,但是它依然了提供容错功能,为大量客户机提供了很高的总体性能. 虽然与 ...

  2. 在page cache中的页,如果当时没有进程read或者write,引用计数到底该为多少

    在一次偶然的机会,在研究如何降低pagecache占用的过程中,走查了 invalidate_mapping_pages的代码: 通过调用 __pagevec_lookup 在radix树中收集一部分 ...

  3. springboot 整合task定时任务

    一步:在启动类中加入     加入就会调用定时了. //开启定时任务 开启后就可以被扫描到   @EnableScheduling 二步:建一个tasks工具包 都会被扫描到的了 有三个类 Async ...

  4. 通过DOS命令批量重命名文件

    以下为提供的两种方法:遍历当前目录下的所有文件名以.avi结尾的文件,然后权限规则进行修改(规则含义请自行查找资料).第一种方法有缺陷,更改完所有的文件名后,会多改一次.请斟酌使用.第二种方法解决了第 ...

  5. Unity3D之Lightmap详解

    作者:李志健 Unity 完全集成了光照贴图,可以通过编辑器创建完整的光照贴图,你完全不用担心,所有材质会自动获得光照贴图.光照贴图的意思是,所有灯光的特性将被直接映射到Beast lightmapp ...

  6. EUREKA原理总结

    Eureka高可用架构 https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance 上图中主要的名称说明: Register:EurekaCli ...

  7. 税控服务器 TC5002UpdatePackage 安装更新

    Linux版税控服务器单税号版本税控应用:   TC5002UpdatePackage2008160711.zip         单税号服务器(型号:TCG-01S1) Linux版税控服务器20个 ...

  8. svn转git

    在Git Bash 中输入 git-svn clone http://devsvnread.uuzuonline.net/GOT_PRIVATE/server/ --no-metadata -T tr ...

  9. 如何解决Android Studio解决DDMS真机/模拟器无法查看data目录问题

    android app开发中,文件.SharedPreference或数据库默认保存在/data文件夹下,有时需要查看该文件夹下数据文件是否创建成功时,发现竟然打不开data目录: 具体解决方式如下: ...

  10. table-cell http://www.cnblogs.com/StormSpirit/archive/2012/10/24/2736453.html

    http://www.cnblogs.com/StormSpirit/archive/2012/10/24/2736453.html