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. GPRS骨干网逻辑结构

    从逻辑上来说,GPRS通过在GSM网络结构中增添SGSN和GGSN两个新的网络节点来实现.由于增加了这两个网络节点,需要命名新的接口.图1说明了GPRS逻辑体系结构.表1给出了GPRS体系结构中的接口 ...

  2. oracle修改sequence,查询sequence信息

    -- 1.查询sequence当前值. select serviceproviderid.nextval from dual; -- 2.查询sequence最大值SELECT SEQUENCE_NA ...

  3. docker开启远程访问

    作者:zqifa 出处:http://www.cnblogs.com/zqifa/ 默认情况下,Docker守护进程Unix socket(/var/run/docker.sock)来进行本地进程通信 ...

  4. 开发环境 pyenv

    pyeny githup地址:https://github.com/pyenv/pyenv/ 安装时使用pyeny提供安装工具进行安装 githup 地址:https://github.com/pye ...

  5. Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)

    Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...

  6. 【HDFS API编程】图解客户端写文件到HDFS的流程

  7. java8-函数编程

    在Function里面看到一个很奇怪的代码 static <T> Function<T, T> identity() { return t -> t; } 手动写了几个代 ...

  8. The requested URL /xxxx.html was not found on this server

    The requested URL /xxxx.html was not found on this server 的原因是,Apache没有开启重写 apache 打开 httpd.conf 文件找 ...

  9. JHipster简介

    JHipster简介 JHipster或者称Java Hipster,是一个应用代码产生器,能够创建Spring Boot + AngularJS的应用.开源项目地址:JHipster/Github. ...

  10. leetcode1033

    class Solution: def numMovesStones(self, a: int, b: int, c: int) -> 'List[int]': l = list() l.app ...