当前位置:网站首页>[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
边栏推荐
- Maxcompute SQL 的查询结果条数受限1W
- Idea one click log generation
- Unrecognized VM option ‘‘
- 2022 le fichier CISP - Pte (i) contient:
- 2018 mathematical modeling competition - special clothing design for high temperature operation
- AHB2APB桥接器设计(2)——同步桥设计的介绍
- Write an example of goroutine and practice Chan at the same time
- 高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression
- Meaning of 0.0.0.0:x
- TiDB 基本功能
猜你喜欢

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

高斯分布Gaussian distribution、线性回归、逻辑回归logistics regression

Centos7.9 install MySQL 5.7 and set startup

【毕业季】毕业是人生旅途的新开始,你准备好了吗

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

Idea one click log generation

Park and unpark in unsafe

Redis cache penetration, cache breakdown, cache avalanche

路由器和交换机的区别

浅谈GPU:历史发展,架构
随机推荐
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
Block level elements & inline elements
pytorch Default process group is not initialized
2022 CISP-PTE(一)文件包含
[QT dot] QT download link
Assembly language - Wang Shuang Chapter 11 flag register - Notes
Compatibility comparison between tidb and MySQL
The fourth question of the 299th weekly match 6103 Minimum fraction of edges removed from the tree
2022 CISP-PTE(一)文件包含
multiprocessing.pool详解
Get the query parameter in the address URL specify the parameter method
Visual Studio VS 快捷键使用大全
面试官:请你介绍一下缓存穿透、缓存空值、缓存雪崩、缓存击穿的,通俗易懂
Distribution gaussienne, régression linéaire, régression logistique
TiDB的使用限制
DMU software syntax highlighting VIM setting -- Learning Notes 6
面试官:用分库分表如何做到永不迁移数据和避免热点问题?
Maxcompute SQL 的查询结果条数受限1W
TiDB 中的数据库模式概述
TiDB 中的SQL 基本操作