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. Galaxy2D游戏引擎常见问题解答

    ◆Galaxy2D游戏引擎开源吗?    Galaxy2D游戏引擎不开源. ◆Galaxy2D相对HGE有何优点?    Galaxy2D相对HGE有以下优点:     (1)自带音频播放功能,HGE ...

  2. bat批量修改图片的名字实现(两种方法)

    问题描述: 业务中遇到需要批量修改大量图片的名字. 如下图,需要修改为图片名字“u=”之后和“,”之前的那一串 解决思路1: bat批处理,网上查找相关代码如下: @echo off SetLocal ...

  3. k2datas 基础编程题,判断字符串是否有重复串

    package String; public class DuplicateString { public static boolean isDup(String s) throws Exceptio ...

  4. send_keys results in Expected 【object Undefined】undefined to be a string解决方法:更新selenium+geckodriver+firefox

    很久之前在win10上配置的测试环境: python 3.6.1+ selenium 3.3.3+ geckodriver 0.15.0以前run case是正常的,今天去run 同样的case时发现 ...

  5. (整理)REHL6.5_安装本地yum

    1.检查是否安装yum包 查看RHEL是否安装了yum,若是安装了,那么又有哪些yum包:rpm -qa |grep yum 2 删除redhat自带的yum包 rpm -qa|grep yum|xa ...

  6. golang初识4 - Go 并发

    Go的CSP并发模型实现:M, P, G Go实现了两种并发形式.第一种是大家普遍认知的:多线程共享内存.其实就是Java或者C++等语言中的多线程开发.另外一种是Go语言特有的,也是Go语言推荐的: ...

  7. winserver2012 下安装 sqlserver2008

    sqlserver2008 安装要求 winserver2012 必须装 .netframework3.5. 但是 winserver2012下,安装 .netframework3.5 ,必须要 wi ...

  8. 关于JWTtoken的管理问题

    JWT简介: Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准.因为网络上有很多关于jwt的详细介绍了,所以我这里就不再赘述.但是JWT的大 ...

  9. Echarts属性大全(及时更新最新信息)

    echarts属性的设置(完整大全)   // 全图默认背景  // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87c ...

  10. 利用selenium爬取京东商品信息存放到mongodb

    利用selenium爬取京东商城的商品信息思路: 1.首先进入京东的搜索页面,分析搜索页面信息可以得到路由结构 2.根据页面信息可以看到京东在搜索页面使用了懒加载,所以为了解决这个问题,使用递归.等待 ...