原题链接在这里: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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode Closest Binary Search Tree Value

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. JavaScript事件大全3

    //无模式的提示框 //屏蔽按键 <html> <head>    <meta http-equiv="Content-Type" content=& ...

  2. php 获取当前脚本的url的案例

    关于用php 获取当前脚本的url很多朋友会说很简单,但是要获取很详细的就要经过多次判断. $PHP_TIME = time();$PHP_SELF = isset($_SERVER['PHP_SEL ...

  3. HDU 5521 Meeting(虚拟节点+最短路)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  4. cordova -v 报错,必须用sodu cordova -v

    问题来源:MAC Caption EI 安装nodejs 4.5.0 之后,用命令:sudo npm install -g cordova ionic 安装cordova 和 ionic 然后用命令: ...

  5. metasploit 常用命令汇总

    1.连接数据并显示一些信息 db_connet username:password@ip address/database name db_destroy 同上 db_import 文件名字 db_h ...

  6. 红米note3的wifi断流或假死

    红米note3的wifi断流/假死 日常使用note3的时,比如长时间浏览网页,点击一个链接会卡住不动,在等待十几秒之后才恢复.第一反应是不是网络不好?但是这种情况常常出现之后,对比其他的手机,比如价 ...

  7. ubuntu下用virtualbox安装windows虚拟机

    按照这个教程: http://jingyan.baidu.com/article/eae07827856ac21fed54856f.html 安装. 会出现问题:"VT-x is disab ...

  8. HDU 1176 经典dp

    记录最晚时间 从time为2枚举到最晚时间 每个时间段的x轴节点都等于上一个时间段的可触及的最大馅饼数 #include<stdio.h> #include<string.h> ...

  9. Javascript 笔记与总结(2-15)结构、样式、行为分离

    [例] <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  10. artDialog ( v 6.0.2 ) content 参数引入页面 html 内容

    /*! artDialog v6.0.2 | https://github.com/aui/artDialog */ 将页面某一隐藏的 div 的 html 内容传到 artdialog 的弹窗中,并 ...