原题链接在这里: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.

题解:

target 一定在找的path 上,若是出现了比minDiff 还小的 差值,就把改点更新为cloest.

但注意target 是 double, 会有溢出情况,所以minDiff 初始化成Double.MAX_VALUE.

Time Complexity: O(h). h 是树的高度. Space: O(1).

AC 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 -1;
} int cloest = root.val;
double minDiff = Double.MAX_VALUE; while(root != null){
if(Math.abs(root.val - target) < minDiff){
minDiff = Math.abs(root.val - target);
cloest = root.val;
}
if(target > root.val){
root = root.right;
}else if(target < root.val){
root = root.left;
}else{
return root.val;
}
}
return cloest;
}
}

跟上Closest Binary Search Tree Value II

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

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

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

  3. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  4. Closest Binary Search Tree Value I & II

    Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the v ...

  5. [LeetCode] 272. 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 ...

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

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

  8. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  9. [Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II

    Closest Binary Search Tree Value  Given a non-empty binary search tree and a target value, find the ...

随机推荐

  1. android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到

    android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到 今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION ...

  2. Maya Shortcuts 常用快捷键

    快捷键 功能解释 工具操作 enter 完成当前操作 ~ 终止当前操作 insert 插入工具编辑模式 w 移动工具 e 旋转工具 r 缩放工具 y 非固定排布工具 shift+Q 选择工具,(切换到 ...

  3. Intent学习笔记

    Intent首先字面意思大概是意图.负责activity之间或者,activity与service等(我只知道这么点)之间信息的传递.就跟快递员起的作用差不多(我这这么理解),由一下六部分组成: Co ...

  4. 一种少见的跨目录写webshell方法

    http://hi.baidu.com/kwthqquszlbhkyd/item/480716204cfa33c3a5275afa

  5. Reset RequiredFieldValidator 重置 验证控件

    <td style="width:100px;">姓名<span class="must_star">*</span>< ...

  6. jQuery 中 on 方法-----给未来元素添加事件

    <li class='clear dir-li'> <div class='left title'> 添加到目录:</div> <div class='lef ...

  7. Delphi如何打开DBF数据库

    Delphi语言,无论Delphi7.Delphi2007或者Delphi XE2或3,无需安装其它附加的部件,就可以实现DBF文件的打开及相关操作,网络上很多要用到什么ADO引擎的,其实未必,只有安 ...

  8. 【转】Java 5种字符串拼接方式性能比较。

    最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...

  9. document.execCommand(”BackgroundImageCache”, false, true)

    很多时候我们要给一些按钮或是img设置背景,而为了达到数据与表现样式分离的效果,通常背景样式都是在CSS里设定的,但是这个行为在IE会有一 个Bug,那就是因为 IE默认情况下不缓存背景图片,所以当鼠 ...

  10. 处理PHP字符串的10个简单方法;mysql出现乱码:character_set_server=utf8

    PHP处理字符串的能力非常强大,方法也是多种多样,但有的时候你需要选择一种最简单且理想的解决方法.文章列举了10个PHP中常见的字符串处理案例,并提供了相对应的最理想的处理方法. 1.确定一个字符串的 ...