[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 ...
随机推荐
- dubbo 搭建以及使用笔记
公司使用dubbo做为rpc框架,有必要简单学习一番(自己搭建),自己做下学习记录. dubbo认识: dubbo是分布式SOA(面向服务)架构的rpc服务治理框架.可兼容各种rpc,强势的地方主要还 ...
- RH_KABI_RESERVE的使用
struct mm_struct { .......... #if defined(__GENKSYMS__) || !defined(CONFIG_SPAPR_TCE_IOMMU) /* We're ...
- ES6的export和import
export import 的4种搭配 非默认 拿函数举例,常量,变量,类也可以 // 1 可以多个export--------import带上{} export var a="123&qu ...
- Hibernate 再接触 ID生成策略
Xml 方法 在student.hbm.xml中 <generator class="uuid"></generator> 取值如下 1.identity: ...
- 03_java基础(六)之CRUD实现
1.简单实现 package com.day01.station.dao; /** * Created by Administrator on 2018/2/1. */ import java.sql ...
- 如何使用eclipse创建JAVA项目并写一个简单的HelloWorld
输入项目名称 点击完成(Finish) 原文地址:https://blog.csdn.net/qq_36798713/article/details/79530056
- smtp扫描
nc扫描 nc -nv ip号 25 nmap扫描
- CentOS7.x安装flash
1.配置 yum 源 sudo rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-x86_64-1.0-1.noa ...
- oracle取某字符串字段的后4位
参考 https://zhidao.baidu.com/question/2142799026528780468.html select substr('str1234', -4) from dual
- 彻底弄懂css中单位px和em,rem的区别
PX:PX实际上就是像素,用PX设置字体大小时,比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,如果改变了浏览器的缩放,这时会使用我们的Web页面布局被打破.这样 ...