[抄题]:

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. ecmall类关系图(转)

  2. Eclipse git插件使用

    1.Eclipse git插件使用 1)配置提交用户名和邮箱 2)在eclipse中选择Show View 搜索git 3)点击clone按钮 选择代码保存路径 4)导入项目 5)git插件功能介绍 ...

  3. 题目1413:神秘元素 ——lis的元素是否唯一

    求序列的最长子序列中不可分割元素的数目.不可分割元素,肯定属于某一个最长子序列,首先做的就是把属于最长子序列的数提取出来,减小查找范围.怎么提取?可以用LIS(最长递增子序列)和LDS(最长递减子序列 ...

  4. nagios(centreon)监控Linux日志

    1 将check_log3.pl下载后放到客户端服务器的插件文件夹[root@SSAVL2475 libexec]# cp /tmp/check_log3.pl  /usr/local/nagios/ ...

  5. Spring Boot和Spring cloud

    微服务框架SpringBoot简单验证 首先摘录部分IBM网站部分内容对框架做一个简单说明 http://www.ibm.com/developerworks/cn/java/j-lo-spring- ...

  6. git log 中文乱码的解决方案

    设置 Git 支持 utf-8 编码在命令行下输入以下命令:$ git config --global core.quotepath false # 显示 status 编码$ git config ...

  7. 以太坊客户端Geth命令用法

    命令用法 geth [选项] 命令 [命令选项] [参数…] 命令: account 管理账户attach 启动交互式JavaScript环境(连接到节点)bug 上报bug Issuesconsol ...

  8. Spring Framework中常见的事务传播陷阱(译文)

    最近看到Medium上一篇讨论Spring Framework中事务传播的文章,解释了几种常见的问题,解释的不错,这里直接翻译吧(意译为主,粗体和斜体是我自己加上的). 译文: 这是我的第一篇文章,我 ...

  9. thinkphp中的配置与读取C方法详解

    1.项目公共配置 Conf/config.php 内容如下 <?php /** *项目公共配置 *@package *@author **/ return array( 'LOAD_EXT_CO ...

  10. linux 利用nethogs查看某进程的网卡流量

    一.nethogs介绍 分享一个linux 下检测系统进程占用带宽情况的检查.来自github上的开源工具. 它不依赖内核中的模块.当我们的服务器网络异常时,可以通过运行nethogs程序来检测是那个 ...