[抄题]:

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. bzoj 2178 圆的面积并——辛普森积分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2178 把包含的圆去掉.横坐标不相交的一段一段圆分开算.算辛普森的时候预处理 f( ) ,比如 ...

  2. CentOS配置网易163 yum源

    使用说明 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...

  3. RK3288 修改浏览器默认主页和书签

    path:packages/apps/Browser/res/values/strings.xml 修改浏览器默认主页: <!-- The default homepage. --> &l ...

  4. Android 使用adb查看和修改电池信息

    1.获取电池信息 $ adb shell dumpsys battery $ adb shell dumpsys battery Current Battery Service state: AC p ...

  5. 安装FreePBX的ISO版本

    下载地址:http://schmoozecom.com/distro-download.php 这个相当于系统了,第一步:安装程序会提示选择你想安装Asterisk的版本:现在出现了11版本,这个根据 ...

  6. 【BZOJ】1012: [JSOI2008]最大数maxnumber /【洛谷】1198(线段树)

    Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插 ...

  7. 堆、栈、free

    转自:http://codeup.org/archives/212 http://bbs.bccn.net/thread-82212-1-1.html http://www.cppblog.com/o ...

  8. [CSAPP] The Unicode Standard for text coding

    The ASCII is only suitable for encoding English-language documents. It's hard for us to encode the s ...

  9. 基于aop的redis自动缓存实现

    目的: 对于查询接口所得到的数据,只需要配置注解,就自动存入redis!此后一定时间内,都从redis中获取数据,从而减轻数据库压力. 示例: package com.itliucheng.biz; ...

  10. 解决 mysql 数据库 挂掉了

    问题 : mysql运行几天之后就挂掉了 , 修改了mysql 的连接数也解决不了,看代码也没有什么问题,但就是感觉哪个功能一直占着mysql资源,查了一下当前的线程状态 time的单位是 秒 , 可 ...