zoukankan      html  css  js  c++  java
  • LeetCode 230. Kth Smallest Element in a BST

    原题链接在这里:https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/

    题目:

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

    Note: 
    You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

    Follow up:
    What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

    Hint:

      1. Try to utilize the property of a BST.
      2. What if you could modify the BST node's structure?
      3. The optimal runtime complexity is O(height of BST).

    题解:

    Method 1: BST 用Binary Tree Inorder Traversal出来的就是由小到大,Method1用recursion,会出来一整个list, 然后返回list.get(k-1)即可.

    Time Complexity: O(n). Space: O(n).

    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 public class Solution {
    11     public int kthSmallest(TreeNode root, int k) {
    12         List<Integer> ls = new ArrayList<Integer>();
    13         inorder(root,ls);
    14         return ls.get(k-1);
    15     }
    16     private void inorder(TreeNode root, List<Integer> ls){
    17         if(root == null){
    18             return;
    19         }
    20         inorder(root.left,ls);
    21         ls.add(root.val);
    22         inorder(root.right,ls);
    23     }
    24 }

    Method 2: 改recursion为iteration, 如此可以不用走完全树.

    Time Complexity: O(n). Space: O(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 int kthSmallest(TreeNode root, int k) {
    12         Stack<TreeNode> stk = new Stack<TreeNode>();
    13         while(root!=null || !stk.empty()){
    14             if(root != null){
    15                 stk.push(root);
    16                 root = root.left;
    17             }else{
    18                 TreeNode tn = stk.pop();
    19                 k--;
    20                 if(k == 0){
    21                     return tn.val;
    22                 }
    23                 root = tn.right;
    24             }
    25         }
    26         return -1;
    27     }
    28 }

    Method 3: 根据BST性质,看root左子树有多少点,如果正好等于k-1, 则返回root.val, 如果小于k-1, 就在root右子树中找第k-leftSum-1小的点值, 若果大于k-1, 就在左子树中找第k小的点值。

    Note: 是返回root.val 不是root.

    Time Complexity: O(n). Space: O(logn).

    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 public class Solution {
    11     public int kthSmallest(TreeNode root, int k) {
    12         int leftSum = nodeSum(root.left);
    13         if(leftSum == k-1){
    14             return root.val;
    15         }else if(leftSum < k-1){
    16             return kthSmallest(root.right,k-leftSum-1);
    17         }else{
    18             return kthSmallest(root.left,k);
    19         }
    20     }
    21     private int nodeSum(TreeNode root){
    22         if(root == null){
    23             return 0;
    24         }
    25         return nodeSum(root.left)+nodeSum(root.right)+1;
    26     }
    27 }
  • 相关阅读:
    深度系统安装wine
    Android应用真正的入口在哪里?
    推荐系统算法概览
    Android面试题:Scrollview内嵌一个Button他的事件消费是怎样的?Move,Down,Up分别被哪个组件消费?
    Java面试题:多线程交替打印字符串
    EventBus使用初体验
    Andoird面试题:A活动启动B活动,他们的生命周期各是怎么样的?
    Android在开发过程中如何选择compileSdkVersion,minSdkVersion和targetSdkVersion
    华为2020暑期实习面经(已拿Offer)
    工商银行软件开发中心2020暑期实习面经(已拿Offer)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4824978.html
Copyright © 2011-2022 走看看