[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 ...
随机推荐
- easyUi弹出window窗口传值与调用父页面的方法,子页面给父页面赋值
<!-- 父页面 --> <!DOCTYPE html PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN" "ht ...
- Windows系统封装总结
注:使用虚拟机或者实体机进行封装均可,实体机进行封装的成功率更高.虚拟机进行封装建议使用VMware,12版本.过高的版本容易造成封装失败 一. Windows 10系统封装 1 ...
- LeetCode OJ 47. Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- 在Centos 6.5 X64下切割m3u8
操作系统:centos 6.5 必需要参考的文章: http://blog.chinaunix.net/uid-23069658-id-4018842.html 准备工作: 安装git yum ins ...
- 连接mysql连接不上遇到的问题
连接不上mysql ,启动mysqld进程,发现可以启动成功,但几秒后进程立马关闭了,后来发现主要原因是因为磁盘空间满了. 报错: Can't connect to local MySQL serve ...
- git 分支合并develop 重新拉取
1.本地包 zb-local 上传到对应的线上分支 zb-online cd 进入本地所在文件夹,然后执行下面三项 git add . git commit -m '修改' git push 或者 ...
- 设置HTML编码为UTF-8
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- LinQ to sql 各种数据库查询方法
1.多条件查询: 并且 && 或者 || var list = con.car.Where(r => r.code == "c014" || r.oil == ...
- TensorFlow 语法
dataset = tf.data.TextLineDataset(file_path) 生成一个dataset,dataset中的每一个元素就对应了文件中的一行
- Android无法访问本地服务器(localhost/127.0.0.1)的解决方案
[Android无法访问本地服务器(localhost/127.0.0.1)的解决方案] 在Android开发中通过localhost或127.0.0.1访问本地服务器时,会报Java.NET.Con ...