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 ...
随机推荐
- MyEclipse基本配置及优化【MyEclipse_10.7】
MyEclipse基本配置 MyEclipse所有的配置都是基于工作空间的,当换了个工作空间,之前的所有配置信息就失效了.(建议每次换工作空间之前就配置好基本信息) 1.修改工作空间编码为UTF-8 ...
- Capture 设置pin name 不可见
在器件库文件页面空白处双击,自动弹出OPTIONS对话框,再设计PIN NAME 可见属性为FLASE. 双击管脚.不管用,改不了.
- Selenium(4)
练习1:使用selenium+firefox测试ecshop登录过程 一.WebDriver 1.启动浏览器 (1)启动Firefox浏览器 a.启动默认路径下的浏览器 WebDriver drive ...
- python: 错误处理try详解 ,traceback调用栈, 调试(logging)
摘录:https://www.liaoxuefeng.com/wiki/1016959663602400/1017598873256736 错误处理 调试 错误处理 高级语言都会使用内置的一套try. ...
- veeValidate实战
说在前面 vee-validate 版本2.0.4的学习github地址我的项目地址第一次认真的在git上写一个demo教程,喜欢的可以star一下~^o^~ (^-^) (^o^) 后续会有一个完整 ...
- Acwing-164-可达性统计(拓扑排序, 位运算统计)
链接: https://www.acwing.com/problem/content/166/ 题意: 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量. 思路: 先拓扑排序求 ...
- Mybatis的@UpdateProvider注解的使用(转)
废话不多说,直接上代码 @UpdateProvider(type = AppProvider.class, method = "updateApp") Integer update ...
- js 获取对应的url参数
举例:http://localhost:5000/?sss=sdf&fdd=123 1.设置或获取整个 URL 为字符串 window.location.href 值: http://loc ...
- Error from server (NotFound): the server could not find the requested resource (get services http:heapster:)
kubectl top pod --all-namespaces Error from server (NotFound): the server could not find the request ...
- .NET面试题系列(二十一)C#中Equals和==的比较
序言 值类型的比较 ; ; Console.WriteLine("Equals和= =(等于号)的比较"); Console.WriteLine("i.Equals(j) ...