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. C#中替换特殊字符串

    private string change字符(string str) { if (!string.IsNullOrEmpty(str)) { //将特殊字符全部替换为下划线 string patte ...

  2. Sublime Text 3安装Package Control

    Package Control官网:https://packagecontrol.io/installation#st3 ctrl + ` 或者 View > Show Console呼出控制台 ...

  3. Centos7上安装java

    1.默认情况下,系统安装的有jdk1.8,但是使用javac测试时会不识别这个命令,   意味着,java还是未安装好. 2.把java升级即可,此时java已经可用. yum -y install ...

  4. Python全栈开发记录_第五篇(装饰器)

    单独记录装饰器这个知识点是因为这个知识点是非常重要的,必须掌握的(代码大约150行). 了解装饰器之前要知道三个知识点 作用域,上一篇讲到过顺序是L->E->G->B 高阶函数: 满 ...

  5. Android 开发 蓝牙开发

    前言 蓝牙开发其实分2个部分,一个是正常蓝牙功能的开发(比如Android蓝牙的互相连接.读取蓝牙列表.文件传输.蓝牙耳机等等).另外一个是BLE蓝牙开发(属于低功耗蓝牙设备,设备大多是血糖仪.蓝牙手 ...

  6. spring boot 整合 云之讯 demo

    ---恢复内容开始--- package com.zhourong.controller; import org.apache.commons.lang3.RandomStringUtils; imp ...

  7. Linux如何查看端口

    Linux如何查看端口 1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 # lsof -i:8000 COMMAND PID USER ...

  8. JavaScript学习-5——异步同步、回调函数

    ----------异步同步函数 ----------回调函数 一.异步同步函数 同步:发送一个请求,等待返回,然后再发送下一个请求 异步:发送一个请求,不等待返回,随时可以再发送下一个请求 同步可以 ...

  9. Centos7】hostnamectl 设置主机名

    Centos7中提供了设置主机名的工具 hostnamectl hostname有三种状态 static(永久) transient(瞬态) pretty (灵活) 查看主机名状态 [oracle@h ...

  10. java学习--修饰符

    Java语言提供了很多修饰符,主要分为以下两类: 访问修饰符 非访问修饰符 访问控制修饰符 访问控制修饰符用来修饰类和类内部的成员变量和成员方法,来确定其访问权限 类的访问控制修饰符只有两种 defa ...