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 a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.
Note:
  • 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.
Follow up:
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的更多相关文章

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

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

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

  4. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

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

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

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

  8. LeetCode Closest Binary Search Tree Value

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...

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

随机推荐

  1. OC-ARC

    一. 基本简介 ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain.release.autorelease语句.你不再需要担心内存管理, ...

  2. Yii2.0 GridView 新增添加按钮

    <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'col ...

  3. 好看的CSS按钮

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. MYSQL的锁介绍,以及死锁发生情况-带例子

    mysql锁能在并发情况下的mysql进行更好的优化 MySQL有三种锁的级别:页级.表级.行级,这3种锁的特性可大致归纳如下: 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高 ...

  5. hdu4982 Goffi and Squary Partition (DFS解法)

    BestCoder Round #6 B http://acm.hdu.edu.cn/showproblem.php?pid=4982 Goffi and Squary Partition Time ...

  6. [PHP知识点乱炖]四、全局变量——小偷从良记

    本章要讲的是PHP的全局变量. 这里讲个小故事: 很多年前,一个很聪明的小偷,想去偷一户人家的钱.可是他偷不到主人的钥匙,怎么办呢? 他想到了一个办法,去之前嚼了一块口香糖,口香糖的牌子是“大大泡泡糖 ...

  7. Windows疑难杂症之开机无法显示桌面。

    开机无法显示桌面可能有以下两种情况. 1.系统故障或病毒引起explorer.exe无法加载启动. 2.注册表故障造成默认的值不是explorer.exe.(可能是安装了某些软件造成此问题) 3,某开 ...

  8. Apache服务器httpd.exe进程占用cpu超过50%的解决方法

    httpd.exe进程占用cpu超过50%,关闭掉Apache服务,cpu应用率立刻下降到0.  重新启动Apache又出现占用cpu高的情况.  原因是:httpd.exe和防火墙配置有冲突. 解决 ...

  9. JS/HTML 保存图片到本地:HTML <a> download 属性

    JS如何保存图片到本地呢?自己百度一下吧! 这里想要说的是,可以利用 HTML 的 <a> 标签 来是实现保存图片到本地的功能,参考代码如下: <a href="http: ...

  10. BZOJ1367——[Baltic2004]sequence

    1.题目大意:给一个序列t,然后求一个序列z,使得$|z1-t1|+|z2-t2|+...+|zn-tn|$的值最小,我们只需要求出这个值就可以了,并且z序列是递增的 2.分析:这道题z序列是递增的, ...