原题链接在这里: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. log4j与commons-logging,slf4j的关系

    前面有一篇日志中简单的介绍了 log4j,同时也介绍了它与commons-logging的关系,但是突然冒出来一个slf4j,并且slf4j有取代commons-logging的趋势,所以,我们可以推 ...

  2. [百科] - SIP(会话发起协议)

    SIP(会话发起协议)SIP是类似于HTTP的基于文本的协议.SIP可以减少应用特别是高级应用的开发时间.由于基于IP协议的SIP利用了IP网络,固定网运营商也会逐渐认识到SIP技术对于他们的深远意义 ...

  3. appSetting 在单独文件的读写

    #region appSetting /// <summary> /// 设定 appSetting /// </summary> /// <param name=&qu ...

  4. Struts2 实战(一)

    环境: Ubuntu 14.04 LTS 64位 开发工具的准备 我选择 Eclipse, 而没有选择MyEclipse, 一是因为免费,不想去弄破解,二是不想太傻瓜化的东西(注:本人并没有用过MyE ...

  5. DNS服务器的配置与应用: BIND9 的安装与配置

    3. BIND9 的安装与配置 3.1 bind简介 BIND (Berkeley Internet Name Domain)是Domain Name System (DNS) 协议的一个实现,提供了 ...

  6. 【新产品发布】《EVC8021 RS-232<>RS-485/422 隔离接口转换器》

    [数据手册下载] 1.百度云盘:(把下面蓝色连接复制到浏览器下打开) http://pan.baidu.com/s/1eQlJ0zC 2.淘宝公司的淘云盘:(点击下面连接后,需要用淘宝账户登录) ht ...

  7. 用Editplus开发Java

    ☆ 准备工作 ①,已安装好jdk,同时配置系统变量(3个,JAVA_HOME,PATH,CLASSPATH) ②,电脑已安装Editplus,并做好设置. ☆ Editplus配置java开发环境 对 ...

  8. Go项目的目录结构说明

    一.项目目录结构 GoPath    /bin    /pkg    /src project_1      project_2 ...... project_n GoPath : 相当于donet下 ...

  9. storm在linux系统下安装调试

    安装: 安装 zookeeper : 下载 zookeeper :http://zookeeper.apache.org/releases.html#download. 将 zookeeper-3.4 ...

  10. Redis 笔记与总结3 list 类型

    redis 版本 [root@localhost ~]# redis-server --version Redis server v= sha=: malloc=jemalloc- bits= bui ...