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 ...
随机推荐
- mongodb与mysql命令对比
mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...
- JS中的闭包的一些理解!
在日常的开发中,基本上是不会遇到关于闭包的这样,但是,因为在一些特殊的情况下,必须采用闭包,所以这里简单的概述下什么是闭包: OK!简而言之,闭包只是一个名词而已,我们更注重于他所实现的功能,也就是我 ...
- java向图片上写字,两个图片合并的方法
package writeimg; import javax.imageio.ImageIO; import java.awt.Color; import java.awt.Font; import ...
- td也可以溢出隐藏显示
或许我这篇文章一取这样的名字,就会有人要问了:你怎么还在关注table啊,那早就过时了…赶紧Xhtml…div好…ul好…ol好…dl好…完了,不知道还有什么好了. table真的过时了么?你真的了解 ...
- state配置语言实战
修改配置文件:(base用来放初始化环境.prod用来放生产配置环境) [root@super65 ~]# vim /etc/salt/master [root@super65 ~]# mkdir - ...
- 连接mysql遇到的问题
1. 首先使用一个用户名不行时,可以新建一个用户名点击用户,输入名和密码,此时一定要记住密码,别忘了.不过也可以点击编辑用户改.此时连接时发现还是连接不上.出现错误信息为: Access denied ...
- Leetcode | Palindrome
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- Scrum会议9(Beta版本)
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- 18. 求交错序列前N项和
求交错序列前N项和 #include <stdio.h> int main() { int numerator, denominator, flag, i, n; double item, ...
- UIButton的遍历
for (id obj in self.view.subviews) { if ([obj isKindOfClass:[UIButton class]]) { ...