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)?

又是只会暴力求解的我T T。

最简单的一个思路就是递归带一个order map,key是差值,value是一个vector,把所有差值存起来。最后遍历一边,因为一定是只有K个解,所以只要遍历到结果数组中元素是K个就停。

递归的时间是O(n),遍历的时间也是O(n)

Runtime 20ms  Beats: 5.34%

当然存在更好的解法啦。

题目说小于o(n),那一定有o(logn)的解法,想想也是,如果这不是一颗树,是一个数组应该怎么找呢,首先用lowerbound找到这个值,然后向两边扩散依此加入K个。

那如果是一颗树,应该也是可以这么做的,过程稍微复杂一点,参考了StefanPochmann大神的帖子。Stefan大神每一个帖子都能让我有重生的感觉(这代码是人写出来的??),真的很佩服他。

在此先截一张图,看一看大家对Stefan的敬仰

哈哈哈哈

好了言归正传,先把代码放上来

基本思路就是,首先我们找到一条通往与target最近的一个path,记录下所有节点,然后向两边延申。

难点在于,树怎么延伸?其实问题转化为,在一个二叉搜索树中给定一个节点,怎么求比他大的第一个节点和比他小的第一个节点。

求比他大的第一个节点

分类讨论,如果这个节点存在右子节点,那么比他大的第一个节点就是右子节点的最左节点,否则,不断的回溯之前的路径,直到前一个节点

不是当前节点的右子节点(而是他的左子节点),因为当当前节点和路径的前一个节点是父节点和左子节点的关系时才会存在父节点大于原来路径中的节点的情况。

求比他小的就呼之欲出了

stefan没有重新写一遍,而是用了两个lambda函数,交换了一下位置,就把意思反过来了。

之后就是不断的更新即可。

整个过程,寻找path o(log(n)),向两边沿申最坏情况也是O(log(n))因为题目已经说了是平衡二叉树。

下面给出我的实现。

另一种解法是中序遍历,然后利用两个双端队列,小于k的存一个,大于k的存另一个,然后从两边展开,这个思路好像更容易一点,而且竟然时间更快,

理论上因为有中序遍历,这个时间复杂度是O(n)的。

LC 272. Closest Binary Search Tree Value II 【lock,hard】的更多相关文章

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

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

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

  5. [leetcode]272. Closest Binary Search Tree Value II二叉搜索树中最近的值2

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

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

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

  8. LeetCode Closest Binary Search Tree Value II

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

  9. [Swift]LeetCode272. 最近的二分搜索树的值 II $ 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. OpenResty 执行流程阶段

    nginx有11个处理阶段,如下图所示: 指令 所处处理阶段 使用范围 解释 init_by_luainit_by_lua_file loading-config http nginx Master进 ...

  2. three.js之创建坐标系网格

    <!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>My first thr ...

  3. 合并K个sorted list

    合并k个已经排好序的数列是面试中也比较容易被问到的一个算法,有很多种解决,其中第一时间比较容易想到的解法如下: 对于这三组从小到大的数列: 如此循环,最终就将三个已经排序的数列的数字按从小到大的顺序排 ...

  4. IQ基础

      I: in-phase 表示同相Q: quadrature 表示正交,与I 相位差90 度. 现在来解释IQ信号的来源: 最早通讯是模拟通讯,假设载波为cos(a),信号为cos(b),那么通过相 ...

  5. jquery中.each()方法遍历循环如何终止方法

    使用return false 终止循环 function checkStar(obj){ var flag=false; //获取本节点星级 var star = obj.getAttribute(& ...

  6. 第二章 Vue快速入门--9 使用v-on指令定义Vue中的事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  7. CF875F Royal Questions[最大生成基环树森林]

    这题这场比赛一堆人秒切..果然还是我太菜了吗 题意:二分图,右边$m$个点每个点$i$向左边有且仅有两条连边,边权都是$a_i$.求最大匹配. 一个朴素思想,二分图匹配,用贪心带匈牙利搞一搞,但是复杂 ...

  8. iconv命令

    iconv 用法: Usage: iconv [OPTION...] [FILE...] Convert encoding of given files from one encoding to an ...

  9. BZOJ 4128: Matrix (矩阵BSGS)

    类比整数的做法就行了 1A爽哉 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int M ...

  10. 题解 最长上升子序列 LIS

    最长上升子序列 LIS Description 给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的 ...