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 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】的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- LeetCode Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- [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 ...
随机推荐
- tomcat启动程序乱码和tomcat启动程序的标题乱码处理
启动程序运行中的文字乱码: 解决方案: 找到Tomcat目录下conf文件夹中的logging.properties文件, 打开logging.properties文件,找到文件中的java.util ...
- 关于MySQL服务无法正常启动问题
使用mysql的时候,突然查看服务列表也找不到mysql服务 解决办法: 一.首先打开CMD,切换到MySql安装目录的MySql Server →bin目录下 运行如下命令(具体试个人安装的MySq ...
- S19格式
S-record格式文件是Freescale CodeWarrior编译器生成的后缀名为.S19的程序文件,是一段直接烧写进MCU的ASCII码,英文全称问Motorola format for EE ...
- 从n个数里面找最大的两个数理论最少需要比较
答案是:n+logn-2 过程是这样的: 甲乙比甲胜出,丙丁比丙胜出,最后甲丙比较,甲胜出...容易得出找出最大数为n-1次. 现在开始找出第二大的数字:明显,第二大的数字,一定和甲进行过比较.... ...
- Linux计划任务与压缩归档
计划任务分为两种形式 第一种:定时性的:也就是例行,每隔一定的周期就要重复来做这个任务. 第二种:突发性的:临时决定,只执行一次的任务. 用到的命令有两个 at:它是一个可以处理仅执行一次的任务就结束 ...
- PHP通过php-java-bridge调用JAVA的jar包里class类
正 文: 有的时候我们需要在PHP里调用JAVA平台封装好的jar包里的class类和方法,一般飘易推荐的做法是采用php-java-bridge做桥接,本文就来介绍一下大致的实现方法. 先简单说 ...
- CSS基础学习-10.CSS伸缩盒(老版本)
- 第七章 路由 82 名称案例-使用keyup事件实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- vmware虚拟机新增磁盘及挂载详细步骤
虚拟机新增磁盘及挂载步骤 1.新增磁盘 (1) 编辑虚拟机设置->添加 (2) 选择硬盘->下一步 (3) 选择SCSI格式,下一步 (4) 创建新虚拟磁盘,下一步 (5) 设置 ...
- (十二)zabbix监控redis
1)agent端配置 安装redis yum install epel-release -y yum install redis -y 配置认证密码 #vim /etc/redis.conf requ ...