当前位置:网站首页>[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
边栏推荐
- Classical cryptosystem -- substitution and replacement
- Get the query parameter in the address URL specify the parameter method
- 快速实现蓝牙iBeacn功能详解
- Unsafe中的park和unpark
- Win10 remote connection to ECS
- Centos7.9 install MySQL 5.7 and set startup
- poi导出excle
- Assembly language - Wang Shuang Chapter 11 flag register - Notes
- tar: /usr/local:归档中找不到tar: 由于前次错误,将以上次的错误状态退出
- 一线大厂面试官问:你真的懂电商订单开发吗?
猜你喜欢
随机推荐
Mathematical modeling contest for graduate students - optimal application of UAV in rescue and disaster relief
面试官:用分库分表如何做到永不迁移数据和避免热点问题?
TiDB 中的SQL 基本操作
Instance tunnel use
快速实现蓝牙iBeacn功能详解
Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
Park and unpark in unsafe
获取地址url中的query参数指定参数方法
0.0.0.0:x的含义
路由器和交换机的区别
网关状态检测 echo request/reply
Machine learning
The fourth question of the 299th weekly match 6103 Minimum fraction of edges removed from the tree
TiDB 基本功能
建模竞赛-光传送网建模与价值评估
Information System Project Manager - Chapter VII project cost management
Tidb database Quick Start Guide
古典密码体制--代换和置换
[QT] use structure data to generate read / write configuration file code
Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes









