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 to the target.
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要用主函数+ DFS来做。错了,“最近”还是直接用二分法左右查找得了
[一句话思路]:
定义一个res,如果root离target的距离小 就替换成为新的res
尾递归变迭代 写法更清楚简单
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 再次出错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 二叉搜索树中,距离目标值最近的节点的更多相关文章
- [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 ...
- [LC] 700题 Search in a Binary Search Tree (二叉搜索树中的搜索) (二叉搜索树)
①中文题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 在上述示例 ...
- [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 ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...
- Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...
- [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 ...
- [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 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- pat1022__字符串查找
主要是对字符串的查找,为了方便并且快速的实现查找,用map会比较方便 同时如何把一个带有空格的字符串变成多个单词也有一个小技巧 char *point=book[i].keyWord;//关键词分离 ...
- vss和vs2008组合搭建源代码管理器
用源代码管理项目,是为了方便开发和管理组内项目,一个组做的是同一套项目,彼此知道各个模块的进度和开发情况,这也是开发项目所需要的.今天整理了VSS的安装.创建.连接及添加项目等操作. 一.安装VSS( ...
- c#实现QQ群成员列表导出及邮件群发之群列表及群成员获取
主题已迁移至:http://atiblogs.com/ ITO-神奇的程序员
- Zookeeper--集群管理
Zookeeper--集群管理 在多台服务器组成的集群中,需要监控每台服务器的状态,一旦某台服务器挂掉了或有新的机器加入集群,集群都要感知到,从而采取相应的措施.一个主动的集群可以自动感知节点的死亡和 ...
- virtual Box centos7 公司网络环境下不能联网的解决方案
首先感谢@采蘑菇的东峰的博客 的分享 原文:http://blog.sina.com.cn/s/blog_8d92d7580102vhky.html ------------------------- ...
- thinkphp 3.2.3 计划任务具体实现实例教程
thinkphp 3.2.3 计划任务具体实现实例教程 很多情况下,我们网站都会用到计划任务即定时更新做一些处理,类似Discuz后台的计划任务,比如更新每日发帖数目等等! 这里TP也是可以实现的,首 ...
- 升级oracle 9i到10g
从9i升级到10g两个方案可供选择: 一是利用oracle提供的一个升级实用程序dbua(database upgrade assistant)直接将9i的数据库升级到10g. 再有就是新建一个1 ...
- 13.solr学习速成之IK分词器
IKAnalyzer简介 IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包. IKAnalyzer特性 a. 算法采用“正向迭代最细粒度切分算法”,支持细粒度和最大词长两 ...
- [z]IE6各种不兼容问题
http://blqw1.diandian.com/post/2012-01-12/16137399 http://www.cnblogs.com/qiangspecial/archive/2013/ ...
- TCP超时与重传机制
TCP超时与重传机制 TCP协议是一种面向连接的可靠的传输层协议,它保证了数据的可靠传输,对于一些出错,超时丢包等问题TCP设计的超时与重传机制.其基本原理:在发送一个数据之后,就开启一个定时器 ...