• 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 }
  • 相关阅读:
    CF521D Shop
    AGC033D Complexity
    CF576D Flights for Regular Customers
    LG4781 【模板】拉格朗日插值
    洛谷3288 SCOI2014方伯伯运椰子(分数规划+spfa)
    洛谷4606 SDOI2018战略游戏(圆方树+虚树)
    洛谷4630APIO2018铁人两项(圆方树+dp)
    CF487E Tourists + 圆方树学习笔记(圆方树+树剖+线段树+multiset)
    CF193D Two Segments (线段树+dp)(外加两个扩展题)
    洛谷4322 SHOI2014 三叉神经树(LCT+思维)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4824978.html
Copyright © 2020-2023  润新知