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. confusing c++ 重写 与 重定义 记录1

    class parent { public: void f() { cout << "parent f()" << endl; } void f(int i ...

  2. mysql decimal

    可能做程序的人都知道,float类型是可以存浮点数(即小数类型),但是float有个坏处,当你给定的数据是整数的时候,那么它就以整数给你处理. 这样我们在存取货币值的时候自然遇到问题,我的defaul ...

  3. ReactiveX 学习笔记(10)可连接的数据流

    Connectable Observable Operators 本文的主题为处理 Connectable Observable 的操作符. 这里的 Observable 实质上是可观察的数据流. R ...

  4. 遍历DOM树,链式操作

    如果需要在同一个选取结果上使用多个jQuery方法,可以同时列出这些方法,并用.隔开,如下面的代码. 1 $("#one").hide().delay(500).fadeIn(15 ...

  5. 【转】Classful IPv4 addressing definition

    Classful addressing definition Class Leadingbits Size of networknumber bit field Size of restbit fie ...

  6. MVC异步控制器加载一个网页的所有内容

    public void PageAsync() { AsyncManager.OutstandingOperations.Increment(); WebRequest req = WebReques ...

  7. django中使用mysql数据库的事务

    django中怎么使用mysql数据库的事务   Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起 ...

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

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

  9. JAVA语言 第五周

    我准备在下一周对Java语法进行总结,现在写代码模板还要参考,语法掌握的不熟悉. 这一周除了对代码进行完善外,观看了一些java入门学习视频.

  10. create-react-app之proxy

    [create-react-app之proxy] create-react-app可以用于一键创建web_client环境,默认使用webpack-dev-server.但在开发过程中,往往需要cli ...