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

    package LeetCode_230
    import java.util.*
    /**
     * 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.
     *
    Example 1:
    Input: root = [3,1,4,null,2], k = 1
      3
     / 
    1   4
     
      2
    Output: 1
    
    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?
    
    Constraints:
    1. The number of elements of the BST is between 1 to 10^4.
    2. You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
     * */
    class TreeNode(var `val`: Int){
        var left: TreeNode?=null
        var right: TreeNode?=null
    }
    class Solution {
        /*
        * solution: traverse tree via inorder and check the index, because its sorted after inorder
        * Time complexity:O(n), Space complexity:O(n)
        * */
        fun kthSmallest(root_: TreeNode?, k: Int): Int {
            var root = root_
            val stack = Stack<TreeNode>()
            var count = 0
            //inorder to find result
            while (stack.isNotEmpty() || root!=null){
                if (root!=null){
                    stack.push(root)
                    root = root.left
                } else {
                    root = stack.pop()
                    count++
                    if (count==k){
                        return root.`val`
                    }
                    root = root.right
                }
            }
            return 0
        }
    }
  • 相关阅读:
    DBCC修复不同情况下的损坏
    Python单例模式的4种实现方法
    osx 安装redis
    tornado系列文章
    Python中__init__和__new__的区别详解
    Tornado源码分析之http服务器篇
    tornado.ioloop.IOLoop相关文章
    How to use POST method in Tornado?
    https://github.com/diogobaeder/pycket
    Frequently Asked Questions
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13417333.html
Copyright © 2011-2022 走看看