原题链接在这里: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. cJSON应用举例

    //在网上查了不少cJSON,结果只找到c语言字符串转换到JSON的实例,想转回来结果没有实例.自己琢磨了一个下午才敢下手.下面把转来转去的代码贴上. //百度网盘的 CJSON 实例源码 地址 ht ...

  2. RN组件之ListView

    /** * Created by DaGuo on 2016/4/7. */ 'use strict' import React,{ Component, View, Text, ListView, ...

  3. 将textField编辑完内容作为参数发送请求

    将textField编辑完内容作为参数发送请求  首先赋值默认值  其次把编辑完的内容传给model,这样的话,model里面的数据就是编辑完之后的内容了

  4. [zt]给你的Mp4大换血,精选Touch里3年收集的900多首歌,"经典不忍去的""最新近流行的",与你共享~~

    如果你是音乐爱好者: 这些歌, 请戴上耳机, 调大音量, 一个人听 ,全世界 都是你的!!!!! (一)这些歌很温暖,没有金属味,适合有阳光的午后,很悠闲... [Anaesthesia]Maximi ...

  5. CentOS源列表

    vi /etc/yum.repos.d/CentOS-Base.repo CentOS 5: # CentOS-Base.repo # # The mirror system uses the con ...

  6. Ubuntu 启动停止脚本

    /etc/init.d 目录下的开机启动脚本 1. more redis_8010 #/bin/sh #Configurations injected by install_server below. ...

  7. Visual Studio解决方案及项目的配置

    配置解决方案的属性 1.配置解决方案平台,该配置实际上修改的是解决方案目录下的sln(solution)文件. 配置项目的属性 1.配置项目平台及项目的目标平台:项目-右键-属性-生成(竖着第二个选项 ...

  8. zabbix 2.2.2在centos 6.3 x86_64上的安装

    zabbix 2.2.2在centos 6.3 x86_64上的安装   更新五月 03, 2014     # 依赖环境 yum install -y php-mbstring mysql-deve ...

  9. 非模态对话框的PreTranslateMessage() 没有用,无法进去

    非模态对话框的的PreTranslateMessage确实进不去, 自然也无法用重载PreTranslateMessage的方法来响应键盘消息. 可以用Hook的方法来使其生效. http://bbs ...

  10. final关键字+const关键字

    final关键字 1.如果我们希望某个类不被其它的类来继承(可能因为安全考虑),可以使用final. 例题 <? final class A{} class B extends A{};//会报 ...