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.

Example:

Input: root = [4,2,5,1,3], target = 3.714286, and k = 2

    4
/ \
2 5
/ \
1 3 Output: [4,3]

Follow up:
Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?

题意

和之前一样,不过这次要找的是最接近的k个值。

Solution1:

1.  Based on BST's attributes, if we traversal BST inorder,  each node will be in acsending order

2. we choose a data structure which can help operate on both sides(LinkedList or Deque), maintaining a K size sliding window

once there is a new item,

we check diff (1) next item vs target

(2) leftMost item vs target

code

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ /*
Time Complexity: O(n)
Space Complexity:O(k)
*/
class Solution {
// choose a data structure which can do operations on both sides
LinkedList<Integer> result = new LinkedList<>(); public List<Integer> closestKValues(TreeNode root, double target, int k) {
// corner case
if (root == null) {return null;}
// inorder traversal
closestKValues(root.left, target, k); if (result.size() < k) {
result.add(root.val);
// maintain a K size sliding window such that items are closest to the target
} else if(result.size() == k) {
if (Math.abs(result.getFirst() - target) > (Math.abs(root.val - target))) {
result.removeFirst();
result.addLast(root.val);
}
}
// inorder traversal
closestKValues(root.right, target, k);
return result;
}
}

[leetcode]272. Closest Binary Search Tree Value II二叉搜索树中最近的值2的更多相关文章

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

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

  3. [LeetCode#272] Closest Binary Search Tree Value II

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

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

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

  6. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  7. [LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆

    描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...

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

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

随机推荐

  1. 常用内置函数_eval_exce

    内置函数 abs 绝对值函数 res = abs(-19) print(res) round 四舍五入(n.5时, 奇数进偶数不进) sum 计算序列的和(元素是数字类型) listvar = (1, ...

  2. 使用 ping++做支付的流程

    获取支付凭据 /// <summary> /// 获取支付凭据 /// </summary> /// <param name="model">& ...

  3. electron+antd详细教程

    第一步: 要做一个electron项目,理论上我们应该从electron-quick-start开始,就是说我们需要如下3个文件: package.json,node工程最基本的要求,类似于Java的 ...

  4. bootstrapvalidator的简单校验【必填校验、长度校验、是否存在校验(remote)】

    需求:输入框的"供应商编码"不能为空而且不能与数据库重复,供应商名称不能为空. 解决: 1.input如下 <input id="ssupplierNo" ...

  5. Akka详细介绍

    AKKA NOTES - 介绍演员 任何在过去做过多线程的人都不会否认管理多线程应用程序有多么困难和痛苦.我说管理因为它开始很简单,一旦你开始看到性能改进,它变得非常有趣.但是,当您发现没有更简单的方 ...

  6. Thinkphp5.0支付宝支付扩展库类库大全

    Thinkphp5.0支付宝支付扩展库类库大全,包括手机网站支付.电脑网站支付.支付查询.退款.退款查询.对账单等. Thinkphp5.0支付宝调用方法: 电脑网站支付 Pagepay.php 调用 ...

  7. Oracle降低高水位先(转载)

    Oracle  降低高水位线的方法 高水位(HIGH WARTER MARK,HWM)好比水库中储水的水位,用于描述数据库中段的扩展方式.高水位对全表扫描方式有着至关重要的影响.当使用DELETE删除 ...

  8. [Linux]CentOS与终端破墙

    来源:https://www.zybuluo.com/ncepuwanghui/note/954160 参考:https://blog.huihut.com/2017/08/25/LinuxInsta ...

  9. Java面试题 BAT 大公司面试题整理总结!

    本文只列出了问题,答案还是需要需要自己的总结,很多时候自己总结出来的语言在面试时比硬背的效果好很多. 这些题目是网友去百度.小米.乐视.美团.58.猎豹.360.新浪.搜狐等一线互联网公司面试被问到的 ...

  10. 容器——list(双向链表)

    做了一道list可以解决的题,才发现list多么的好 转自https://www.cnblogs.com/BeyondAnyTime/archive/2012/08/10/2631191.html 1 ...