zoukankan      html  css  js  c++  java
  • LeetCode 272. 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:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public List<Integer> closestKValues(TreeNode root, double target, int k) {
    12         LinkedList<Integer> res = new LinkedList<>();
    13         if(root == null || k <= 0){
    14             return res;
    15         }
    16         
    17         inorder(root, target, k, res);
    18         return res;
    19     }
    20     
    21     private void inorder(TreeNode root, double target, int k, LinkedList<Integer> que){
    22         if(root == null){
    23             return;
    24         }
    25         
    26         inorder(root.left, target, k, que);
    27         if(que.size() == k){
    28             if(Math.abs(que.peek() - target) > Math.abs(root.val - target)){
    29                 que.pollFirst();
    30             }else{
    31                 return;
    32             }
    33         }
    34         
    35         que.add(root.val);
    36         inorder(root.right, target, k, que);
    37     }
    38 }

    找出一个最小值,从BST中删掉这个key. 

    Time Complexity: O(k*logn).

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public List<Integer> closestKValues(TreeNode root, double target, int k) {
    12         List<Integer> res = new ArrayList<Integer>();
    13         if(root == null){
    14             return res;
    15         }
    16         
    17         for(int i = 0; i<k ;i++){
    18             int closest = closestValue(root, target);
    19             res.add(closest);
    20             root = deleteNode(root, closest);
    21         }
    22         
    23         return res;
    24     }
    25     
    26     private int closestValue(TreeNode root, double target){
    27         if(root == null){
    28             return Integer.MAX_VALUE;
    29         }
    30         int closest = root.val;
    31         double minDiff = Double.MAX_VALUE;
    32         while(root != null){
    33             if(Math.abs(root.val - target) < minDiff){
    34                 minDiff = Math.abs(root.val - target);
    35                 closest = root.val;
    36             }
    37             
    38             if(target > root.val){
    39                 root = root.right;
    40             }else if(target < root.val){
    41                 root = root.left;
    42             }else{
    43                 return root.val;
    44             }
    45         }
    46         return closest;
    47     }
    48     
    49     private TreeNode deleteNode(TreeNode root, int key){
    50         if(root == null){
    51             return root;
    52         }
    53         
    54         if(root.val > key){
    55             root.left = deleteNode(root.left, key);
    56         }else if(root.val < key){
    57             root.right = deleteNode(root.right, key);
    58         }else{
    59             if(root.left == null){
    60                 return root.right;
    61             }else if(root.right == null){
    62                 return root.left;
    63             }
    64             
    65             int suc = findSuc(root.right);
    66             root.val = suc;
    67             deleteNode(root.right, suc);
    68         }
    69         return root;
    70     }
    71     
    72     private int findSuc(TreeNode root){
    73         int suc = root.val;
    74         while(root.left != null){
    75             root = root.left;
    76             suc = root.val;
    77         }
    78         return suc;
    79     }
    80 }

    类似Closest Binary Search Tree Value

  • 相关阅读:
    D3.js比例尺 定量比例尺 之 线性比例尺(v3版本)
    D3.js的基础部分之数组的处理 集合(Set)(v3版本)
    D3.js的基础部分之数组的处理 映射(Map)(v3版本)
    D3.js (v3)+react框架 基础部分之认识选择集和如何绘制一个矢量图
    D3.js的一些基础部分 (v3版本)
    Flask 教程 第九章:分页
    Flask 教程 第八章:粉丝
    Flask 教程 第七章:错误处理
    Flask 教程 第六章:个人主页和头像
    Flask 教程 第五章:用户登录
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5204366.html
Copyright © 2011-2022 走看看