LeetCode Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/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 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.
Follow up:
Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?
题解:
Inorder traversal. When que size == k, if que.peekFirst() is further from target, poll first. Otherwise, that means que.peekFirst() is closer, then there is no need to add current root val and there is no need to iterate root.right side, because it is even larger, and even further from target.
Time Complexity: O(n). Space: O(k). 若果不考虑recursion stack.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> closestKValues(TreeNode root, double target, int k) {
LinkedList<Integer> res = new LinkedList<>();
if(root == null || k <= 0){
return res;
} inorder(root, target, k, res);
return res;
} private void inorder(TreeNode root, double target, int k, LinkedList<Integer> que){
if(root == null){
return;
} inorder(root.left, target, k, que);
if(que.size() == k){
if(Math.abs(que.peek() - target) > Math.abs(root.val - target)){
que.pollFirst();
}else{
return;
}
} que.add(root.val);
inorder(root.right, target, k, que);
}
}
找出一个最小值,从BST中删掉这个key.
Time Complexity: O(k*logn).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> closestKValues(TreeNode root, double target, int k) {
List<Integer> res = new ArrayList<Integer>();
if(root == null){
return res;
} for(int i = 0; i<k ;i++){
int closest = closestValue(root, target);
res.add(closest);
root = deleteNode(root, closest);
} return res;
} private int closestValue(TreeNode root, double target){
if(root == null){
return Integer.MAX_VALUE;
}
int closest = root.val;
double minDiff = Double.MAX_VALUE;
while(root != null){
if(Math.abs(root.val - target) < minDiff){
minDiff = Math.abs(root.val - target);
closest = root.val;
} if(target > root.val){
root = root.right;
}else if(target < root.val){
root = root.left;
}else{
return root.val;
}
}
return closest;
} private TreeNode deleteNode(TreeNode root, int key){
if(root == null){
return root;
} if(root.val > key){
root.left = deleteNode(root.left, key);
}else if(root.val < key){
root.right = deleteNode(root.right, key);
}else{
if(root.left == null){
return root.right;
}else if(root.right == null){
return root.left;
} int suc = findSuc(root.right);
root.val = suc;
deleteNode(root.right, suc);
}
return root;
} private int findSuc(TreeNode root){
int suc = root.val;
while(root.left != null){
root = root.left;
suc = root.val;
}
return suc;
}
}
类似Closest Binary Search Tree Value.
LeetCode Closest Binary Search Tree Value II的更多相关文章
- [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 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- LeetCode Closest Binary Search Tree Value
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...
- [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] 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 ...
- [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 ...
- 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 ...
随机推荐
- Java学习第一步: Win7配置JDK环境
转摘"专注JavaWeb开发":http://www.javaweb1024.com/java/Javajichu/2015/03/02/353.html 下载JDK并安装到本机 ...
- 使用edtftpj-***.jar上传下载中文问题的解决方案和注意点
FileTransferClient ftpClient = null; try { ftpClient = new FileTransferClient(); // set remote host ...
- shadowColor表示阴影颜色,shadowBlur表示模糊等级
绘制之前的准备工作: 1.在body中加入canvas标签,设置它的id.width.height,当然也可以动态设置它的宽高. <canvas id="mycanvas" ...
- mysql时该如何估算内存的消耗,公式如何计算?
经常有人问配置mysql时该如何估算内存的消耗.那么该使用什么公式来计算呢? 关心内存怎么使用的原因是可以理解的.如果配置mysql服务器使用太少的内存会导致性能不是最优的;如果配置了太多的内存则会导 ...
- 用Get-ADComputer取非常用属性的值
由于GE使用的是Windows2003+Powershell2.0, 所以某些命令无法使用,比如想取lastLogon和lastLogonTimestamp这两个属性,在Powershell3.0下可 ...
- Sql Server 常用方法、存储过程备用
常用方法 --字符串转换成数字 --CAST("1" AS int) --CONVERT(int,"1") --截取字符串 SUBSTRING(OccurreA ...
- etcd学习记录
参考资料: etcd:从应用场景到实现原理的全方位解读 etcd:用于服务发现的键值存储系统 Etcd学习(一)安装和.NET客户端测试 Etcd学习(二)集群搭建Clustering
- Linux 计划任务 Crontab 笔记与总结(2)Crontab 的基本组成与配置
[Crontab 的基本组成] ① 系统服务 CROND:每分钟都会从配置文件刷新定时任务 ② 配置文件 :文件方式设置定时任务 ③ 配置工具 crontab:用途调整定时任务 [配置文件的配置文件格 ...
- sql like模糊查询
1.SQL LIKE 子句中使用百分号(%)字符来表示任意字符,类似于UNIX或正则表达式中的星号 (*). 2.LIKE 通常与 % 一同使用,类似于一个元字符的搜索. 3. > SELECT ...
- RT-Thread 线程调度
/* 变量分配4字节对齐 */ ALIGN(RT_ALIGN_SIZE) /* 静态线程的 线程堆栈*/ ]; ]; /* 静态线程的 线程控制块 */ static struct rt_thread ...