LeetCode Closest Binary Search Tree Value
原题链接在这里:https://leetcode.com/problems/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.
题解:
target 一定在找的path 上,若是出现了比minDiff 还小的 差值,就把改点更新为cloest.
但注意target 是 double, 会有溢出情况,所以minDiff 初始化成Double.MAX_VALUE.
Time Complexity: O(h). h 是树的高度. Space: O(1).
AC 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 -1;
} int cloest = root.val;
double minDiff = Double.MAX_VALUE; while(root != null){
if(Math.abs(root.val - target) < minDiff){
minDiff = Math.abs(root.val - target);
cloest = root.val;
}
if(target > root.val){
root = root.right;
}else if(target < root.val){
root = root.left;
}else{
return root.val;
}
}
return cloest;
}
}
跟上Closest Binary Search Tree Value II
LeetCode Closest Binary Search Tree Value的更多相关文章
- [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 ...
- [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
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- Closest Binary Search Tree Value I & II
Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the v ...
- [LeetCode] 272. 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 ...
- [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 ...
- 272. 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 close ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- [Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II
Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the ...
随机推荐
- 20. 求阶乘序列前N项和
求阶乘序列前N项和 #include <stdio.h> double fact(int n); int main() { int i, n; double item, sum; whil ...
- Web 在线文件管理器学习笔记与总结(3)创建文件
① 创建文件 a. 文件名的合法性:不能包含 \/:*"<>| 等特殊字符 b. 检测当前目录下是否存在同名文件,如果存在提示请重命名后创建,如果不存在则直接创建 index.p ...
- 理解 Python 中的 *args 和 **kwargs
Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如: def test_defargs(one, two = 2): print 'Required argument: ', one ...
- PHP生成随机密码的4种方法及性能对比
PHP生成随机密码的4种方法及性能对比 http://www.php100.com/html/it/biancheng/2015/0422/8926.html 来源:露兜博客 时间:2015-04 ...
- 如何开启mysql计划事件
如何开启mysql计划事件 (2012-07-26 12:21:23) 转载▼ 标签: mysql 事件计划 it 分类: MySQL 首先在sql中查询计划事件的状态:SHOW VARIABLES ...
- 20145235李涛 《Java程序设计》第3周学习总结
类与对象 定义类 类是对象的“设计图”,对象是类的实际类型.另外,定义时用class,建实例用new. 通过书上的代码才有所理解: class Clothes { String color; char ...
- 如何解决php 生成验证码图片不显示问题
最近遇到一个问题,就是验证码在别人的电脑上可以显示,但是我自己的电脑上去不能.原因找了好久,哈哈,终于找到了!现在给大家分享一下: 程序: <?php $w = 80; //设置图片宽和高 $h ...
- mysqli_query($link,'SET group_concat_max_len=8192');
mysqli_query($link,'SET group_concat_max_len=8192'); $sql = 'SELECT GROUP_CONCAT(w) FROM ---'; mysql ...
- General protection fault Exceptions in Linux/IA32 Systems
Computer Systems A Programmer's Perspective Second Edition Exception number Description Exception cl ...
- Machine Learning in Action -- AdaBoost
初始的想法就是,结合不同的分类算法来给出综合的结果,会比较准确一些 称为ensemble methods or meta-algorithms,集成方法或元算法 集成方法有很多种,可以是不同算法之间的 ...