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

  • 相关阅读:
    Function overloading and return type
    Function overloading and const keyword
    Function Overloading in C++
    web安全测试相关内容(三)
    web安全测试相关内容(二)
    web安全测试相关内容(一)
    CDNDrive 第一个版本发布 & 布客新知第二次备份完成
    PyTorch 1.4 中文文档校对活动正式启动 | ApacheCN
    布客&#183;ApacheCN 编程/大数据/数据科学/人工智能学习资源 2020.2
    计算机电子书 2019 BiliDrive 备份
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5204366.html
Copyright © 2011-2022 走看看