LeetCode Closest Binary Search Tree Value

时间:2022-09-19 10:16:53

原题链接在这里: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的更多相关文章

  1. &lbrack;LeetCode&rsqb; 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 ...

  2. &lbrack;LeetCode&rsqb; 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 ...

  3. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  4. Closest Binary Search Tree Value I &amp&semi; II

    Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the v ...

  5. &lbrack;LeetCode&rsqb; 272&period; 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 ...

  6. &lbrack;LeetCode&rsqb; 272&period; 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 ...

  7. 272&period; 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 ...

  8. LeetCode&colon; Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  9. &lbrack;Locked&rsqb; Closest Binary Search Tree Value &amp&semi; Closest Binary Search Tree Value II

    Closest Binary Search Tree Value  Given a non-empty binary search tree and a target value, find the ...

随机推荐

  1. re&period;search 和 re&period;match

    相同点: 都返回找到的第一个匹配对象 >>> import re >>> m = re.search('(\w+) (\w+)', 'aaa bbb ccc ddd ...

  2. C&num;的变迁史 - C&num; 5&period;0 之并行编程总结篇

    C# 5.0 搭载于.NET 4.5和VS2012之上. 同步操作既简单又方便,我们平时都用它.但是对于某些情况,使用同步代码会严重影响程序的可响应性,通常来说就是影响程序性能.这些情况下,我们通常是 ...

  3. Python之路【第三篇】:Python基础(二)

    函数的理解 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 函数作用是你的程序有良好的扩展性.复用性. 同样的功能要是用3次以上的话就建议 ...

  4. inline-block 和 float 的区别

    1.float元素会自动成为一个块元素. 2.float元素,会脱离文档流!   默认脱离文档流的元素的z-index值是比没有脱离文档流的元素高的! 3.float:没有上下哦,  上下用margi ...

  5. Qt之QLineEdit

    简述 QLineEdit是一个单行文本输入框. QLineEdit允许用户输入和编辑单行纯文本,提供了很多有用的编辑功能,包括:撤消和重做.剪切和粘贴.以及拖放(见setDragEnabled()). ...

  6. Altera USB Blaster 仿真器(EPM240仿制版

    转载:http://tigerwang202.blogbus.com/logs/35981280.html 其他很好的资料:http://bbs.ednchina.com/BLOG_ARTICLE_1 ...

  7. Android NDK学习总结

    一.android NDK编程步骤 java文件中声明native方法. android工程根目录新建jni文件夹. 调用javah命令为第一步声明的native方法生成相应的.h头文件. 通过win ...

  8. R语言学习网站

    一个不错的个人R语言博客网站 http://blog.fens.me/r-overview/

  9. java学习笔记 --- 多态

    一.多态 (1)定义:同一个对象在不同时刻体现出来的不同状态.父类的引用或者接口的引用指向了自己的子类对象.   Dog d = new Dog();//Dog对象的类型是Dog类型.  Animal ...

  10. Base64图片编码优化

    通过对图片进行base64编码,将base64(或其他数据)内嵌在image标签的属性当中(或者CSS中或JavaScript中),可以实现将图片直接嵌入代码中的目的,如此一来,可以减少HTTP请求, ...