[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 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.
Example:
Input: root = [4,2,5,1,3], target = 3.714286
4
/ \
2 5
/ \
1 3
Output: 4
题目
给定一棵二叉搜索树和一个target,求出其节点中最接近该target的值。
思路
1. based on the BST's attribution ( left < root < right), we use binary search
代码
class Solution {
public int closestValue(TreeNode root, double target) {
int result = root.val;
while(root != null){
//update result if the current value is closer to target
if(Math.abs(target - root.val) < Math.abs(target - result)){
result = root.val;
}
//binary search
root = root.val > target? root.left: root.right;
}
return result;
}
}
[leetcode]270. Closest Binary Search Tree Value二叉搜索树中找target的最接近值的更多相关文章
- 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 ...
- [LC] 700题 Search in a Binary Search Tree (二叉搜索树中的搜索) (二叉搜索树)
①中文题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 在上述示例 ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [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 ...
随机推荐
- unity 4.6.1脚本解析出错,没有激活的勾,方法顺序出错
检查方法声明上的注释:如/**xx*/或/*xx*/改为//形式 没有激活的勾: 1.如/**xx*/或/*xx*/改为//形式 2.必须保留Start函数
- Python系列之 __new__ 与 __init__
很喜欢Python这门语言.在看过语法后学习了Django 这个 Web 开发框架.算是对 Python 有些熟悉了.不过对里面很多东西还是不知道,因为用的少.今天学习了两个魔术方法:__new__ ...
- spring boot集成MyBatis
- 生成一个文件夹中的所有文件的txt列表
1.windows操作系统中 1.用管理员运行打开dos界面: 2.用cd转到相应的文件夹中: 3.用dir /b /on >list.txt来生成文件列表的txt. 2.Mac系统中 1.打开 ...
- js基础-函数基础
js 先对函数进行解析 然后在执行函数 定义一个函数 实现求两个数的乘 function mult(a,b){ return a*b; } mult(1,3) 计算1 - n 的和 封装成函数 fun ...
- Numpy知识(三)
ndarray的花式索引. 正负数索引,正数就是从0开始的下标正向寻找,负数是-1开始的负向寻找. arr[[1,5,2,6],[0,3,1,2]]:拿取arr[1,0],arr[5,3],arr[2 ...
- tomcat 下配置 可 调试
set JAVA_OPTS=-Djute.maxbuffer=2048000 set console_log=true 起始行配置 rem Ensure that any user defined C ...
- 对于“2017面向对象程序设计(Java)第就十周学习总结”存在问题的反馈
1.“学生们普遍反映对泛型相关知识点的理解有一些难度,而且对泛型有关程序的编写有些困难.希望老师再次讲解.同学们普遍反映第四.第五个实验较难,大部分同学不能独立完成实验,希望老师能在课堂上详细解答.根 ...
- Light Explorer
[Light Explorer] The Light Explorer allows you to select and edit light sources. Window> Lighting ...
- Typechecking With PropTypes
[Typechecking With PropTypes] 1.props类型检查 React has some built-in typechecking abilities. To run typ ...