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

Hint:

1. Consider implement these two helper functions:
  i. getPredecessor(N), which returns the next smaller node to N.
  ii. getSuccessor(N), which returns the next larger node to N.
2. Try to assume that each node has a parent pointer, it makes the problem much easier.
3. Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
4. You would need two stacks to track the path in finding predecessor and successor node separately.

270. Closest Binary Search Tree Value 的拓展,270题只要找出离目标值最近的一个节点值,而这道题要找出离目标值最近的k个节点值。

解法1:Brute Force, 中序遍历或者其它遍历,同时维护一个大小为k的max heap。

Java:

/**
* 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) {
LinkedList<Integer> res = new LinkedList<>();
inOrderTraversal(root, target, k, res);
return res;
} private void inOrderTraversal(TreeNode root, double target, int k, LinkedList<Integer> res) {
if (root == null) {
return;
}
inOrderTraversal(root.left, target, k, res);
if (res.size() < k) {
res.add(root.val);
} else if(res.size() == k) {
if (Math.abs(res.getFirst() - target) > (Math.abs(root.val - target))) {
res.removeFirst();
res.addLast(root.val);
} else {
return;
}
}
inOrderTraversal(root.right, target, k, res);
}
}

Java:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private PriorityQueue<Integer> minPQ;
private int count = 0;
public List<Integer> closestKValues(TreeNode root, double target, int k) {
minPQ = new PriorityQueue<Integer>(k);
List<Integer> result = new ArrayList<Integer>(); inorderTraverse(root, target, k); // Dump the pq into result list
for (Integer elem : minPQ) {
result.add(elem);
} return result;
} private void inorderTraverse(TreeNode root, double target, int k) {
if (root == null) {
return;
} inorderTraverse(root.left, target, k); if (count < k) {
minPQ.offer(root.val);
} else {
if (Math.abs((double) root.val - target) < Math.abs((double) minPQ.peek() - target)) {
minPQ.poll();
minPQ.offer(root.val);
}
}
count++; inorderTraverse(root.right, target, k);
}
} 

Java:

/**
* 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) {
PriorityQueue<Double> maxHeap = new PriorityQueue<Double>(k, new Comparator<Double>() {
@Override
public int compare(Double x, Double y) {
return (int)(y-x);
}
});
Set<Integer> set = new HashSet<Integer>(); rec(root, target, k, maxHeap, set); return new ArrayList<Integer>(set);
} private void rec(TreeNode root, double target, int k, PriorityQueue<Double> maxHeap, Set<Integer> set) {
if(root==null) return;
double diff = Math.abs(root.val-target);
if(maxHeap.size()<k) {
maxHeap.offer(diff);
set.add(root.val);
} else if( diff < maxHeap.peek() ) {
double x = maxHeap.poll();
if(! set.remove((int)(target+x))) set.remove((int)(target-x));
maxHeap.offer(diff);
set.add(root.val);
} else {
if(root.val > target) rec(root.left, target, k, maxHeap,set);
else rec(root.right, target, k, maxHeap, set);
return;
}
rec(root.left, target, k, maxHeap, set);
rec(root.right, target, k, maxHeap, set);
}
}

Java: A time linear solution, The time complexity would be O(k + (n - k) logk). Space complexity is O(k).

/**
* 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> result = new ArrayList<>();
if (root == null) {
return result;
} Stack<Integer> precedessor = new Stack<>();
Stack<Integer> successor = new Stack<>(); getPredecessor(root, target, precedessor);
getSuccessor(root, target, successor); for (int i = 0; i < k; i++) {
if (precedessor.isEmpty()) {
result.add(successor.pop());
} else if (successor.isEmpty()) {
result.add(precedessor.pop());
} else if (Math.abs((double) precedessor.peek() - target) < Math.abs((double) successor.peek() - target)) {
result.add(precedessor.pop());
} else {
result.add(successor.pop());
}
} return result;
} private void getPredecessor(TreeNode root, double target, Stack<Integer> precedessor) {
if (root == null) {
return;
} getPredecessor(root.left, target, precedessor); if (root.val > target) {
return;
} precedessor.push(root.val); getPredecessor(root.right, target, precedessor);
} private void getSuccessor(TreeNode root, double target, Stack<Integer> successor) {
if (root == null) {
return;
} getSuccessor(root.right, target, successor); if (root.val <= target) {
return;
} successor.push(root.val); getSuccessor(root.left, target, successor);
}
}  

C++:

class Solution {
public:
vector<int> closestKValues(TreeNode* root, double target, int k) {
vector<int> res;
priority_queue<pair<double, int>> q;
inorder(root, target, k, q);
while (!q.empty()) {
res.push_back(q.top().second);
q.pop();
}
return res;
}
void inorder(TreeNode *root, double target, int k, priority_queue<pair<double, int>> &q) {
if (!root) return;
inorder(root->left, target, k, q);
q.push({abs(root->val - target), root->val});
if (q.size() > k) q.pop();
inorder(root->right, target, k, q);
}
};  

类似题目:

[LeetCode] 270. Closest Binary Search Tree Value 最近的二叉搜索树的值

  

All LeetCode Questions List 题目汇总

最新文章

  1. 在 Laravel 中使用图片处理库 Integration/Image
  2. windows服务器的DDOS防御,
  3. Java数据结构 遍历 排序 查找 算法实现
  4. Java classpath 如何自动添加web-content /lib下的jar包
  5. IT人的自我导向型学习:学习的3个维度
  6. 第一篇帖子,就弄个JS动态公告浏览吧,直接上代码
  7. 《linux文件权限管理大总结》RHEL6
  8. ubuntu安装python3.5
  9. 重绘(redraw或repaint),重排(reflow)
  10. Oracle定时任务小案例
  11. linux 命令之文件读取,head, tail, tailf, sed
  12. eclipse设置
  13. emacs 集成astyle
  14. 理解SQL的左连接与右连接
  15. 【转】利用python的KMeans和PCA包实现聚类算法
  16. Spark在Windows下的环境搭建(转)
  17. apacheTomcat
  18. redis常见应用场景
  19. 接口测试——带token请求post接口(postman学习)
  20. &lt;Spark&gt;&lt;Programming&gt;&lt;Loading and Saving Your Data&gt;

热门文章

  1. laravel 学习之第一章
  2. mysql5.7二进制包进行多实例安装
  3. ui自动化之selenium操作(一)环境搭建
  4. CPU、CPU核与线程的关系
  5. get和post请求的区别?
  6. excel中汉字转拼音
  7. 对GraphQL-BFF:微服务背景下的前后端数据交互方案的研究-------引用
  8. 《Spring源码深度解析》一
  9. Python连接MySQL之Python库pymysql
  10. BeanPostProcessor和BeanFactoryPostProcessor的区别