[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 ...
随机推荐
- kettle 数据库连接失败
kettle 数据库连接失败 测试连接提示缺少驱动. 提示错误信息:Driver class 'oracle.jdbc.driver.OracleDriver' could not be found, ...
- Day 11:静态导入、增强for循环、可变参数的自动装箱与拆箱
jdk1.5新特性-------静态导入 静态导入的作用: 简化书写. 静态导入可以作用一个类的所有静态成员. 静态导入的格式:import static 包名.类名.静态的成员: 静态导入要注意的 ...
- MySQL表的几个简单查询语句
1. 创建数据库CREATE DATABASE database-name 2. 删除数据库drop database dbname 3. 创建新表create table tabname(co ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> ...
- Codeforces Round #620 (Div. 2)E LCA
题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到 ...
- 201771010123汪慧和《面向对象程序设计Java》第二周学习总结
一.理论知识部分 1.标识符由字母.下划线.美元符号和数字组成, 且第一个符号不能为数字.标识符可用作: 类名.变量名.方法名.数组名.文件名等.第二部分:理论知识学习部分 2.关键字就是Java语言 ...
- Vue中Js动画 与Velocity.js 多组件多元素 列表过渡
Vue提供我们很多js动画钩子 写在tansition标签内部 入场动画 @before-enter="" 处理函数收到一个参数(e l) el为这个元素 @enter=" ...
- TypeError: TF_SessionRun_wrapper: expected all values in input dict to be ndarray.
在用Embedding时出现了这个问题,具体的代码: model.add(Embedding(input_dim = vocab_size, output_dim = embedding_vector ...
- PHP学习之-文件上传
一.PHP文件上传 HTML部分 <form action="file_big.php" method="post" enctype="mult ...
- uni-app: 如何实现增量更新功能?
都知道,很多APP都有增量更新功能,Uni APP也是在今年初,推出了增量更新功能,今天我们就来学习一波. 当然,很多应用市场为了防止开发者不经市场审核许可,给用户提供违法内容,对增量更新大多持排斥态 ...