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.
Note:
- Given target value is a floating point.
- You are guaranteed to have only one unique value in the BST that is closest to the target.
链接: http://leetcode.com/problems/closest-binary-search-tree-value/
题解:
求BST中跟target最近的数字。我们先设置一个min = root.val,然后用iterative的办法尝试更新min, 然后比较target与root的大小,进行二分查找。
Time Complexity - O(logn), Space Complexity - O(1)。
/**
* 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) {
int min = root.val;
while(root != null) {
min = Math.abs(target - root.val) < Math.abs(target - min) ? root.val : min;
root = root.val < target ? root.right : root.left;
} return min;
}
}
二刷:
这道题也是主要考察binary search。方法和一刷一样。 可以有递归和迭代。
Java:
迭代
Time Complexity - O(logn), Space Complexity - O(1)。
/**
* 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) {
int min = root.val;
while (root != null) {
min = Math.abs(root.val - target) < Math.abs(min -target) ? root.val : min;
root = target < root.val ? root.left : root.right;
}
return min;
}
}
递归:
/**
* 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) {
TreeNode child = target < root.val ? root.left : root.right;
if (child == null) {
return root.val;
}
int childClosest = closestValue(child, target);
return Math.abs(root.val - target) < Math.abs(childClosest - target) ? root.val : childClosest;
}
}
三刷:
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 0;
int min = root.val;
while (root != null) {
min = (Math.abs(root.val - target) < Math.abs(min - target) ? root.val : min);
root = (root.val < target) ? root.right : root.left;
}
return min;
}
}
Reference:
https://leetcode.com/discuss/54438/4-7-lines-recursive-iterative-ruby-c-java-python
270. Closest Binary Search Tree Value的更多相关文章
- 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 clo ...
- 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]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 ...
- [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 ...
- [LC] 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】270. Closest Binary Search Tree Value 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Indigo Studio
http://www.infragistics.com/products/indigo-studio?gclid=CIXrnav4lcQCFdclvQoduVEAnA
- 结对开发--课堂练习--c++
一.题目与要求 题目: 返回一个整数数组中最大子数组的和. 要求: 入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值. ...
- hibernate tool连接oracle生成pojo和xml文件无法查询表解决办法
需要在hibernate的配置文件中增加 <property name="hibernate.default_schema">[username]</proper ...
- windows环境下svn同步web文件[转]
windows环境下svn同步web文件 SVN在团队开发中使用非常普遍,是一个很方便的版本控制系统. 如果要是能将SVN服务器上的数据自动发布到Web服务器,那将是整个项目开发.测试更加便捷.利用S ...
- 网络服务器带宽Mbps、Mb/s、MB/s有什么区别?10M、100M到底是什么概念?
网络服务器带宽Mbps.Mb/s.MB/s有什么区别?我们经常听到IDC提供的服务器接入带宽是10M独享,或者100M独享,100M共享之类的数据.这的10M.100M到底是什么概念呢? 工具/原料 ...
- netty 粘包问题处理
netty 粘包问题处理 key words: netty 粘包 解包 半包 TCP 一般TCP粘包/拆包解决办法 定长消息,例如每个报文长度固定,不够补空格 使用回车换行符分割,在包尾加上分割符,例 ...
- IDA*
模拟退火 基本思路(Main Thoughts): IDA*是一种优秀的搜索法,在一般的实际问题中,它比普通的搜索更快. 通过迭代加深和估价函数剪枝来搜索. 通常处理没有层数上界或上界很多大的搜索. ...
- HTTP1.1缓存策略
以下是一幅虽然信息包含量有限.但足够以最简洁的方式说明了“什么是HTTP1.1缓存策略”的图 缓存和缓存策略 web缓存(web cache)或代理缓存(proxy cache)是一种特殊的HTTP ...
- [百度空间] [原]DLL导出实例化的模板类
因为模板是在编译的时候根据模板参数实例化的,实例化之后就像一个普通的类(函数),这样才有对应的二进制代码;否则,没有模板参数,那么编译器就不知道怎么生成代码,所以生成的DLL就没有办法导出模板了.但是 ...
- MYSQL导入导出.sql文件(转)
一.MYSQL的命令行模式的设置: 桌面->我的电脑->属性->环境变量->新建-> PATH=“:path\mysql\bin;”其中path为MYSQL的安装路径. ...