Closest Binary Search Tree Value I & II
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.
public class Solution {
public int closestValue(TreeNode root, double target) {
int closest = root.val;
while(root != null){
// 如果该节点的离目标更近,则更新到目前为止的最近值
closest = Math.abs(closest - target) < Math.abs(root.val - target) ? closest : root.val;
// 二叉搜索
root = target < root.val ? root.left : root.right;
}
return closest;
}
}
Leetcode: Closest Binary Search Tree Value II
- Given target value is a floating point.
- You may assume k is always valid, that is: k ≤ total nodes.
- You are guaranteed to have only one unique set of k values in the BST that are closest to the target.
Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Solution {
public List<Integer> closestKValues(TreeNode root, double target, int k) {
PriorityQueue<Double> maxHeap = new PriorityQueue<Double>(k, new Comparator<Double>() {
@Override
public int compare(Double x, Double y) {
return (int) (y - x);
}
});
Set<Integer> set = new HashSet<Integer>();
rec(root, target, k, maxHeap, set);
return new ArrayList<Integer>(set);
}
private void rec(TreeNode root, double target, int k, PriorityQueue<Double> maxHeap, Set<Integer> set) {
if (root == null) return;
double diff = Math.abs(root.val - target);
if (maxHeap.size() < k) {
maxHeap.offer(diff);
set.add(root.val);
} else if (diff < maxHeap.peek()) {
double x = maxHeap.poll();
if (!set.remove((int) (target + x))) {
set.remove((int) (target - x));
}
maxHeap.offer(diff);
set.add(root.val);
} else {
if (root.val > target) {
rec(root.left, target, k, maxHeap, set);
} else {
rec(root.right, target, k, maxHeap, set);
}
return;
}
rec(root.left, target, k, maxHeap, set);
rec(root.right, target, k, maxHeap, set);
}
}
Closest Binary Search Tree Value I & II的更多相关文章
- [Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II
Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the ...
- [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 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 II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- 272. Closest Binary Search Tree Value II
题目: Given a non-empty binary search tree and a target value, find k values in the BST that are close ...
- [LeetCode] 272. Closest Binary Search Tree Value 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 ...
- LeetCode Closest Binary Search Tree Value
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...
- 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 ...
随机推荐
- ubuntu网络设置
修改/etc/network/interfaces文件sudo gedit /etc/network/interfaces 贴出我的eth0设置,自己看情况修改:# The primary netwo ...
- Code Contracts for .NET
https://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970
- maven可选依赖(Optional Dependencies)和依赖排除(Dependency Exclusions)
我们知道,maven的依赖关系是有传递性的.如:A-->B,B-->C.但有时候,项目A可能不是必需依赖C,因此需要在项目A中排除对A的依赖.在maven的依赖管理中,有两种方式可以对依赖 ...
- jQuery checkbox 所有 全选、全不选、是否选中等
下面是网络收集: jquery判断checked的三种方法:.attr('checked): //看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或fals ...
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- xStream完美转换XML、JSON
xStream框架 xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换: 前面有介绍过json-lib这个框架,在线博文:htt ...
- QQ空间HD(2)-UIPopoverController其它使用
DJTestViewController.m #import "DJTestViewController.h" #import "DJColorTableViewCont ...
- php中的正则函数主要有三个-正则匹配,正则替换
php中变量的声明? 由于php声明变量的时候, 不支持使用 var关键字, 又不能直接写一个变量名字, 孤零零的放在那里, 所以, 在php中声明变量的方式, 同时也是给变量初始化的形式, 即: & ...
- 如何获取客户端MAC地址(三个方法)
方法一: 调用Windows的DOS命令,从输出结果中读取MAC地址: public static String getMACAddress() { String address = "&q ...
- [译]Mongoose指南 - Population
MongoDB没有join, 但是有的时候我们需要引用其它collection的documents, 这个时候就需要populate了. 我们可以populate单个document, 多个docum ...