当前位置:网站首页>[leetcode] day90 the element with the smallest K in the binary search tree
[leetcode] day90 the element with the smallest K in the binary search tree
2022-06-27 06:51:00 【It's a circle upside down】
subject
230. Binary search tree K Small elements 【 secondary 】
Answer key
In the sequence traversal
The order traversal in a binary search tree is a sequence of increasing numbers , So the first K The small element is the second element in the binary search tree K The value of nodes , Using the middle order traversal can solve ~
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);
}
}
Time complexity : O ( n ) O(n) O(n)
Spatial complexity : O ( 1 ) O(1) O(1)
p.s The term is finally over ! There will be no classes in the future ! But the efficiency at home is hard to say , also 5 It's getting worse , If I were in school, I would have broken through 100 days
边栏推荐
猜你喜欢

The risk of multithreading -- thread safety

机 器 学 习

Gaussian distribution, linear regression, logistic regression

卷积神经网络---CNN模型的应用(找矿预测)

古典密码体制--代换和置换

Us camera cloud service scheme: designed for lightweight video production scenes

进程终止(你真的学会递归了吗?考验你的递归基础)

研究生数学建模竞赛-无人机在抢险救灾中的优化应用

Information System Project Manager - Chapter VII project cost management
![[QT] use structure data to generate read / write configuration file code](/img/f4/685315c92f4e2528652532445fb519.png)
[QT] use structure data to generate read / write configuration file code
随机推荐
winow10安装Nexus nexus-3.20.1-01
获取地址url中的query参数指定参数方法
高斯分布Gaussian distribution、线性回归、逻辑回归logistics regression
Date database date strings are converted to and from each other
2022 CISP-PTE(一)文件包含
Convolution neural network -- Application of CNN model (ore prospecting prediction)
Scala之偏函数Partial Function
快速实现Thread Mesh组网详解
IDEA一键生成Log日志
TiDB的使用限制
The number of query results of maxcompute SQL is limited to 1W
How to write controller layer code gracefully?
tracepoint
Block level elements & inline elements
win10远程连接云服务器
The risk of multithreading -- thread safety
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
日期 数据库日期 字符串 之间互相转换
Compatibility comparison between tidb and MySQL
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?