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 ...
随机推荐
- cocos2dx 2.0+ 版本,IOS6.0+设置横屏
使用cocos2dx 自带的xcode模板,是不能正常的设置为横屏的. 一共修改了三个地方: 在项目属性中:Deployment Info中,勾选上 Landscape left,以及 Landsca ...
- Daily Scrum6
今天我们小组开会内容分为以下部分: part 1: Anti-spam and anti-abuse module模块总结: part 2: 部分优化代码的展示于交流: part 3:针对用户积分模块 ...
- 不会JS中的OOP,你也太菜了吧!(第二篇)
一.你必须知道的 1> 原型及原型链在继承中起到了关键的作用.所以你一定要理解他们.2> 不会JS中的OOP,你也太菜了吧!(第一篇) 二.继承的6种方法 1> 原型链继承 原型链继 ...
- curl Protocol 'http not supported or disabled in libcurl
C:\Documents and Settings\ganiks.liu\Desktop\curl-7.37.0-win32\bin>curl -V curl 7.37.0 (i386-pc-w ...
- discuz之搭建
本篇将介绍IIS+MySQL+DiscuzX3.1+UCenter1.6+Asp.Net+PHP的部署 大部分都是搬运过来的,当然我会注明搬运地点 搭建 首先说明本机基本信息 系统========== ...
- wrap device
刚刚看见了,wrap device && reference device 区别在这里 https://msdn.microsoft.com/en-us/library/windows ...
- 帝国cms栏目别名如何调用?
我们在用帝国cms建站时经常会发现栏目的标题不好设置,栏目名称太长的话在后台那边看了眼花,太短又不好优化.能不能直接调用栏目别名呢?栏目别名不会什么影响.那么,帝国cms栏目别名怎么调用呢?和ytka ...
- ubuntu学习之一
1终端 在右键后菜单中找到 终端,(如果没有终端,使用 Ctrl+Alt+[F1~F6] ,您可以切换到1~6号控制台,输入: sudo apt-get install nautilus-open-t ...
- xxx.properties获取方法
1.手动获取加载 Locale locale = Locale.getDefault(); ResourceBundle localResource = ResourceBundle.getBundl ...
- Extjs文本输入框
var loginForm = Ext.create('Ext.form.Panel', { title: '单行输入', renderTo: Ext.getBody( ...