当前位置:网站首页>【LeetCode】Day90-二叉搜索树中第K小的元素
【LeetCode】Day90-二叉搜索树中第K小的元素
2022-06-27 06:28:00 【倒过来是圈圈】
题目
题解
中序遍历
二叉搜索树中序遍历就是一个递增数列,因此第K小的元素即二叉搜索树中序遍历的第K个结点的值,用中序遍历就可以解决~
class Solution {
int count=0,res=0;
public int kthSmallest(TreeNode root, int k) {
midOrder(root,k);
return res;
}
public void midOrder(TreeNode root,int k){
if(root==null)
return;
midOrder(root.left,k);
count++;
if(count==k){
res=root.val;
return;
}
midOrder(root.right,k);
}
}
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
p.s 期末终于结束了!以后再也不用上课啦!不过在家的效率真是一言难尽,又5天没更了,要是在学校的话估计早就突破100天了
边栏推荐
猜你喜欢
随机推荐
Tidb basic functions
AHB2APB桥接器设计(2)——同步桥设计的介绍
The risk of multithreading -- thread safety
研究生数学建模竞赛-无人机在抢险救灾中的优化应用
JVM common instructions
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
论文阅读技巧
Us camera cloud service scheme: designed for lightweight video production scenes
Using CSDN to develop cloud and build navigation websites
el-select多个时,el-select筛选选中过的值,第二个el-select中过滤上一个选中的值
Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
Altium Designer 19 器件丝印标号位置批量统一摆放
Multithreading basic part part 1
vscode korofileheader 的配置
[QT notes] simple understanding of QT meta object system
Quick personal site building guide using WordPress
Redis cache penetration, cache breakdown, cache avalanche
Kubesphere cluster configuration NFS storage solution - favorite
Ahb2apb bridge design (2) -- Introduction to synchronous bridge design
Fractional Order PID control








