【LeetCode】230. Kth Smallest Element in a BST
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的更多相关文章
- 【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 ...
- 【刷题-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 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【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 ...
- 【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 ...
- LeetCode OJ 230. Kth Smallest Element in a BST
Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- [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 ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
随机推荐
- django urls 配置小记
django urls 配置小记 首先应了解 django2.0在url的配置上较之以前的版本有点区别,在之前的版本是通过django.conf.urls.url函数来实现路径配置的 urlpatte ...
- SQL Server 两条数据创建时自动关联
begin ),(, ))) from workplan a join org_employee b on b.id = a.idowner , LEN(aa.sglzbbh))) glh from ...
- linux epoll,poll,select
epoll函数用法,还有点poll和select 1,LT的epoll是select和poll函数的改进版. 特点是,读完缓冲区后,如果缓冲区还有内容的话,epoll_wait函数还会返回,直到把缓冲 ...
- [小程序]微信小程序获取位置展示地图并标注信息
1.map组件的高度如果想要铺满屏幕,要是使用height:100vh样式2.获取位置要在app.json中标明权限3.先使用wx.getLocation获取自己的位置,然后再回调中使用setData ...
- kibana和中文分词器analysis-ik的安装使用
Centos7安装elasticSearch6 上面讲述了elasticSearch6的安装和使用教程. 下面讲一下elasticsearch6的管理工具Kibana. Kibana是一个开源的分析和 ...
- sed 常用命令 网址
https://wangchujiang.com/linux-command/c/sed.html https://linux.cn/article-11367-1.html https://juej ...
- flask flask_session,WTForms
一.Flask_session 本质上,就是是cookie 下的session存储在redis中,方便快速取得session from flask import Flask,session from ...
- Java中用import导入类和用Class方法加载类有什么区别?
import仅仅包含导入操作,并不包含将字节码文件加载进内存这一动作,将字节码文件加载进内存是后续的实例化操作完成的. 例如通过import导入了一堆包和类,但是后续什么都没用(没用实例化),那么导入 ...
- 第24课经典问题(中)-----关于const对象的疑问
关于const对象的疑问const关键字能否修饰类的对象?如果可以,有什么特性?const关键字能够修饰对象const修饰的对象为只读对象只读对象的成员变量不允许被改变.(对象是只读的,成员变量不允许 ...
- SHELL脚本和常用命令
什么是脚本? 脚本简单地说就是一条条的文字命令(一些指令的堆积),这些文字命令是可以看到的(如可以用记事本打开查看.编辑). 常见的脚本: JavaScript(JS,前端),VBScript, AS ...