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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int closestValue(TreeNode root, double target) {
if (root == null) {
return -1;
}
int res = root.val;
while (root != null) {
if (Math.abs(root.val - target) < Math.abs(res - target)) {
res = root.val;
}
if (root.val > target) {
root = root.left;
} else {
root = root.right;
}
}
return res;
}
}

[LC] 270. Closest Binary Search Tree Value的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 close ...

  4. [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 t ...

  5. [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 ...

  6. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  7. 【LeetCode】270. Closest Binary Search Tree Value 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. C# 互操作性入门系列(二):使用平台调用调用Win32 函数

    好文章搬用工模式启动ing ..... { 文章中已经包含了原文链接 就不再次粘贴了 言明 改文章是一个系列,但只收录了2篇,原因是 够用了 } --------------------------- ...

  2. java中流的注意事项

    缓冲流 缓冲流继承自过滤流,使用缓冲流时一些要注意的知识点: 1.如果在缓冲流对象创建时使用了其他流,最后关闭时只需关闭缓冲流就可以了,其他流会跟着自动关闭. 2.缓冲字符输入流(BufferedRe ...

  3. 18 12 23 html 基本学习

    一个html的基本结构如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  4. [前端] VUE基础 (9) (element-ui、axios、Vuex)

    一.element-ui的使用 官方网页:https://element.eleme.cn/#/zh-CN 1.安装element-ui (venv) D:\pycharm_workspace\vue ...

  5. (转)ERROR : The processing instruction target matching "[xX][mM][lL]" is not allowed.

    现象:ERROR   : The processing instruction target matching "[xX][mM][lL]" is not allowed. 异常解 ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习:函数参数

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 88.QuerySet API使用详解:get_or_create和bulk_create方法

    get_or_create 根据某个条件进行查找,如果找到了匹配的数据就会返回这条数据,如果没有找到匹配到的数据,就会创建一个.示例代码如下: from django.http import Http ...

  8. kubectl 常用命令一

    1.kubectl logs <options>  <PodName> -f -p, --previous --since= No. --since-time= --tail ...

  9. 使用GitHub Pages服务进行域名URL转发

    有时,你注册了一个域名,但是你没有搭建服务器.你希望这个域名能指向你的主页/博客/微博等.但是,很多域名注册商不提供这种服务,或者这是一项收费服务.这时你可以使用GitHub来实现这一功能. 你需要导 ...

  10. Tooltips2

    #include<windows.h> #include<Commctrl.h> #include"resource.h" #pragma comment( ...