[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为要用主函数+ DFS来做。错了,“最近”还是直接用二分法左右查找得了

[一句话思路]:

定义一个res,如果root离target的距离小 就替换成为新的res

尾递归变迭代 写法更清楚简单

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 再次出错DFS的表达式不能用作赋值(因为会继续循环),所以只能用root赋值

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

DFS的表达式不能用作赋值(因为会继续循环)

[复杂度]:Time complexity: O(lgn) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

找最接近的值:用二分法

[关键模板化代码]:

while类型的dfs还是符合退出+扩展

while (root != null) {
//exit
if (Math.abs(target - root.val) < Math.abs(target - ans)) {
ans = root.val;
}
//expand to left, right
root = (root.val > target) ? root.left : root.right;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

272. Closest Binary Search Tree Value II 最接近的k个数:俩stack 好吧。。

[代码风格] :

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

270. Closest Binary Search Tree Value 二叉搜索树中,距离目标值最近的节点的更多相关文章

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

  2. [LC] 700题 Search in a Binary Search Tree (二叉搜索树中的搜索) (二叉搜索树)

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

  3. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

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

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

  5. 173 Binary Search Tree Iterator 二叉搜索树迭代器

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...

  6. Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...

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

  8. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. Fuel9.0安装openstack过程中所踩过的坑2018最新版

    坑一,安装好后,无法访问Web UI画面 访问https//10.20.0.2:8443无法打开UI画面.首先我们不管以后的步骤,打不开是很不爽的. 解决方法:把下面网卡1,网卡2,网卡3的界面名称都 ...

  2. 项目代码部署百度云(使用git部署,node环境)

    学习做了一个小demo,总是在自己的电脑,和局域网上运行很没意思,现在就做点有意思的事情,将代码部署百度云. 1)首先你得进入百度云(直接百度,先要注册一个账号) 2)点击那个“应用引擎”,就会进入 ...

  3. php的闭包

    闭包是指在创建时封装周围状态的函数,即使闭包所在的环境的不存在了,闭包中封装的状态依然存在. 匿名函数其实就是没有名称的函数,匿名函数可以赋值给变量,还能像其他任何PHP函数对象那样传递.不过匿名函数 ...

  4. Java里的常用运算符及其优先级顺序

    知道了八种基本数据类型后,在使用中弄清运算符的优先级是很有必要的.具体如下图:  这里需要注意的是,强制类型转换的优先级是位于乘除前面而处于单目运算符后面的,这是比较容易出错的地方.比如用Math.R ...

  5. Form 总结

    禁止input自动完成下拉 //ie: autocomplete="off" //ff: disableautocomplete <input size="40&q ...

  6. hive默认分隔符引起的日志分割问题

    Hive中的外部表 对于Hive中的外部表来说,因为表是外部的,Hive认为其并不拥有这份数据,删除该表并不会真正删除其中的数据,其中的表描述元信息会被删除掉.   对数据进行分区后,对于管理表,可以 ...

  7. 020:Buffer Pool 、压缩页、CheckPoint、Double Write、Change Buffer

    一. 缓冲池(Buffer Pool) 1.1 缓冲池介绍 每次读写数据都是通过 Buffer Pool : 当Buffer Pool 中没有用户所需要的数据时,才去硬盘中获取: 通过 innodb_ ...

  8. SpringMVC使用Hibernate-validator验证出现的错误

    缺少jar包 SpringMVC可以使用Hibernate-validator作为效验的实现,需要的jar包: hibernate-validator.jar validation-api.jar j ...

  9. 类名+函数名(参数1,参数2.....){.......return this;}

    下述的函数是这样定义的: 类名+函数名(参数1,参数2.....){.......return this;} int +函数名(参数1,参数2.....){.......return int;} sh ...

  10. PHP中的traits简单理解

    Traits可以理解为一组能被不同的类都能调用到的方法集合,但Traits不是类!不能被实例化.先来例子看下语法: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...