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 }
  • 相关阅读:
    JS基础学习
    Java_iText_PDF—生成PDF工具
    vc++ 编译连接错误and解决方法
    C++ 数据类型【转】
    jsp两种include指令区别
    程序员面试宝典问题及解析
    vc++6.0快捷键
    vue动态绑定图片和背景图
    配置webpack中dev.env.js、prod.env.js,解决不同命令下项目启动和打包到指定的环境
    配置webpack中externals来减少打包后vendor.js的体积
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4824978.html
Copyright © 2011-2022 走看看