Difficulty: Medium

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/kth-smallest-element-in-a-bst/

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.

Example 1:

Input: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
  2
Output: 1

Example 2:

Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
Output: 3

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?

Intuition

Inorder traversal

Solution

    public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> stk = new Stack<>();
while(root!=null || !stk.isEmpty()){
while(root!=null){
stk.push(root);
root=root.left;
}
if(--k==0)
return stk.pop().val;
root=stk.pop().right;
}
return -1;
}

  

Complexity

Time complexity : O(logN+k)

Space complexity : O(logN) the depth of tree

What I've learned

1. Think about inorder traversal when there's a BST.

 More:【目录】LeetCode Java实现

【LeetCode】230. Kth Smallest Element in a BST的更多相关文章

  1. 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

  2. 【刷题-LeetCode】230. Kth Smallest Element in a BST

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

  3. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  4. 【Leetcode】378. Kth Smallest Element in a Sorted Matrix

    Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...

  5. 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)

    Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...

  6. LeetCode OJ 230. Kth Smallest Element in a BST

    Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...

  7. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  8. [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素

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

  9. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

随机推荐

  1. CHIP8模拟器的python3实现-3-指令实现

    class Chip8CPU(object): def __init__(self, screen): self.registers = { 'v': [], 'index': 0, 'pc': 0, ...

  2. ANDROID培训准备资料之Service

    在讨论Service 之前,我们需要了解两点,非常重要的两点 (1)     Service 不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中 (2)     Service也不 ...

  3. Android内部存储与外部存储的文件操作类

    public class SDCardHelper { // 判断SD卡是否被挂载 public static boolean isSDCardMounted() { // return Enviro ...

  4. 如何解决问题:程序无法正常启动(0xc0000022)

    如何解决问题:程序无法正常启动(0xc0000022) 下文是由NeoSmart技术,通过全球网络上的工程师开发者和技术人员一起收集起来的信息,进行汇总并编辑而成的. 错误现象 该错误一般会通过警告, ...

  5. 关于如何自定义修改pytest-html报告深度学习总结

    第一.pytest-html执行命令总结: pytest test_case.py --html=report.html --self-contained-html 直接html独立显示pytest ...

  6. 解决IIS7、IIS7.5 应用程序池回收假死的方法

    最近iis网站一直假死状态,都懵了,查看程序有没有关闭数据库,反复捣鼓,还一直测试是否是程序应用池自动回收问题依然没有效果.经过老师提醒,找到了解决办法,在此做个笔记! 原因在于:应用程序池超时配置被 ...

  7. pikachu SQL-Injection

    1.数字型注入(POST) 可以看到,这个参数提交是POST类型的,用burp. 测试参数id id='&submit=%E6%9F%A5%E8%AF%A2 可以看到有报错回显,而且根据回显可 ...

  8. Java8——Optional

    /* * 一.Optional 容器类:用于尽量避免空指针异常 * Optional.of(T t) : 创建一个 Optional 实例 * Optional.empty() : 创建一个空的 Op ...

  9. webapi基于单请求封装多请求的设计【转】

    怎么说,单请求封装多请求,这句话确实有点绕了,但还是要看清楚,想明白这到底是怎么一回事,单请求即一次请求(get,post,put,delete),封闭多请求,即在客户端发送的一个请求中可能包含多个子 ...

  10. Spring Cloud微服务安全实战_4-3_订单微服务&价格微服务

    实现一个场景: 订单微服务: POM: <?xml version="1.0" encoding="UTF-8"?> <project xml ...