原题链接在这里: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. 省份+城市---Dropdownlist控件的应用

    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> ...

  2. ajax无刷新获取php后台数据

    $.ajax({ url:"result.php", //data:{"page":i}, dataType:"json", beforeS ...

  3. Careercup | Chapter 7

    7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...

  4. svchost.exe是什么?为什么一直在运行

    原文:http://www.howtogeek.com/howto/windows-vista/what-is-svchostexe-and-why-is-it-running/ 自己简单翻译了下,图 ...

  5. PHP 错误与异常 笔记与总结(10)错误处理器测试

    关联文件:myErrorHandler.php (上一篇) 先测试通知级别的错误的自定义处理: testErrorHandler.php <?php require_once 'myErrorH ...

  6. 【翻译】西川善司的「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,后篇

    http://www.4gamer.net/games/216/G021678/20140714079/     连载第2回的本回,  Arc System Works开发的格斗游戏「GUILTY G ...

  7. 【翻译】Kinect Studio是? 三月 SDK Update的新机能

      Kinect应用软件开发支援工具「Kinect Studio」的功能和用法的说明.由于可以记录/再生数据,让开发和调试变得更加简单.   Kinect SDK v2预览版的RTM版的预定在发布之前 ...

  8. Tomcat安装、启动和配置

    Tomcat服务器介绍 Tomcat是一个免费开发源代码的web应用服务器,具有与IIS.Apache等web服务器一样处理html页面的功能,另外它还是一个Servlet和JSP容器,独立的serv ...

  9. hiho42 : 骨牌覆盖问题·二

    描述 上一周我们研究了2xN的骨牌问题,这一周我们不妨加大一下难度,研究一下3xN的骨牌问题?所以我们的题目是:对于3xN的棋盘,使用1x2的骨牌去覆盖一共有多少种不同的覆盖方法呢?首先我们可以肯定, ...

  10. go access database demo

    package main import ( "database/sql" "fmt" _ "github.com/lib/pq" " ...