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:

  1. Input: root = [4,2,5,1,3], target = 3.714286, and k = 2
  2.  
  3. 4
  4. / \
  5. 2 5
  6. / \
  7. 1 3
  8.  
  9. 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. PHP的八种数据类型

    PHP 支持8种基本的数据类型. 四种标量类型: boolean (布尔型) integer (整型) float (浮点型, 也称作 double) string (字符串) 两种复合类型: arr ...

  2. Linux基础命令02

    常用的一些命令选项 向网络发送icmp检测主机是否在线 ping 指定发送包数量 ping -c windows系统中是ping -t不间断刷包 比如ping百度,ping不同,一直卡在这里,加了-w ...

  3. 白话跨域CORS

    跨域访问控制是浏览器和服务器按照约定,协同工作,守护安全的一种机制. 其中认为浏览器和服务器是安全的,但是浏览器上运行的页面(HTML+JS)可能不安全. 分几种不同方式. 页面跨域简单请求(Get/ ...

  4. python+Appium自动化:输入中文问题

    只要接触到app自动化,难免会遇到许多坑,今天说说解决中文输入的问题. 流程: 进入到淘宝应用,点击搜索栏,输入文字 一开始send_keys(“中文”)时,搜索栏一直没有出现文字,脚本也没有提示报错 ...

  5. 内置对象-Math

    1.随机数 Math.random() 1)获得0-1之间的随机数 2)0到100:Math.round(Math.random()*100) 2.max:求最大值 ,,,) console.log( ...

  6. 前端 OSS 自动化部署脚本

    部署脚本 (deploy.js 自己命名) const co = require('co') const OSS = require('ali-oss') const path = require(' ...

  7. Vue上传通过“服务端签名后直传”上传文件到阿里云 报错 400 Bad Request

    我报错的原因是 formData.append('file', file) 放在签名前面了 解决办法 formData.append('file', file) 一定在最后 /** * 上传文件到 o ...

  8. docker 部署springcloud Feign组件无法访问问题

    如题: docker部署后的服务注册ID是这样的. 这导致了Feign在调用其他组件的时候访问不到. 解决: 在docker部署指令添加EUREKA_INSTANCE_IP-ADDRESS=[你的IP ...

  9. mysql 导出导入数据库(Mysqldump)备份

    使用mysql不熟练啊!!! mysqldump导出数据库,必须以cmd命令行的形式,在Navicat中以新建查询形式使用Mysqldump不好使的.(本来使用Navicat转储SQL,再导入SQL, ...

  10. POJ 2109 Power of Cryptography 数学题 double和float精度和范围

    Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21354 Accepted: 107 ...