[LC] 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 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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 【LeetCode】270. Closest Binary Search Tree Value 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [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 ...
- [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 ...
随机推荐
- Vue组件template模板字符串几种写法
在定义Vue组件时,组件的模板template选项需要的是一个字符串,当其内容较复杂需要换行时,需要简单处理一下,具体有五种方式: 方式一:使用 \ 转义换行符 <!DOCTYPE html&g ...
- 抓DHCP客户端ip脚本
cat testnew.sh #!/bin/bash catch_ip (){Ip=`sudo nmap -sP 192.168.1.0/24 |grep -i -B2 $mac|grep Nmap ...
- 分布式CAP定理(转)
在弄清楚这个问题之前,我们先了解一下什么是分布式的CAP定理. 根据百度百科的定义,CAP定理又称CAP原则,指的是在一个分布式系统中,Consistency(一致性). Availability(可 ...
- Python笔记_第五篇_Python数据分析基础教程_NumPy基础
1. NumPy的基础使用涵盖如下内容: 数据类型 数组类型 类型转换 创建数组 数组索引 数组切片 改变维度 2. NumPy数组对象: NumPy中的ndarray是一个多维数组对象,该兑现共有两 ...
- Python笔记_第五篇_Python数据分析基础教程_相关安装和版本查看
1. IDE说明: 所有的案例用Anacoda中的Jupiter工具进行交互式讲解. 2. 版本和安装: NumPy从如下网站安装:http://sourceforge.net/projects/nu ...
- H3C S10512虚拟化配置
软件版本:Version 7.1.070, Release 7585P05 1.配置SW1#设置SW1的成员编号为1,创建IRF端口2,并将它与物理接口Ten-G0/0/45.Ten-G0/0/46. ...
- Okhttp 多次调用同一个方法出现错误java.net.SocketException: Socket closed
Okhttp 多次调用同一个方法出现错误java.net.SocketException: Socket closed https://blog.csdn.net/QQiqq1314/article/ ...
- libcurl在windows下的使用
curl在linux下很好用,但到了windows下写程序却没办法使用了,这时候可以使用libcurl库 libcurl库的编译网上很多,我就不一一赘述了,curl的官方网站:https://curl ...
- h5-过度
1.过度的基本介绍及写法 .div{ width: 200px; height: 200px; background-color: red; position: absolute; left: 100 ...
- JDK的安装与环境变量配置
1.下载JDK后安装,此处安装的是JDK8 2.安装后的路径如下图所示,JDK与JRE在同一个文件夹中 3.安装完JDK后配置环境变量 计算机→属性→高级系统设置→高级→环境变量 4.系统变量→新建 ...