当前位置:网站首页>[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
边栏推荐
- Fractional Order PID control
- Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
- Centos7.9 install MySQL 5.7 and set startup
- Get the query parameter in the address URL specify the parameter method
- [QT dot] QT download link
- 内存屏障今生之Store Buffer, Invalid Queue
- Overview of database schema in tidb
- Spark sql 常用时间函数
- 如何优雅的写 Controller 层代码?
- Winow10 installation nexus nexus-3.20.1-01
猜你喜欢

Quick realization of Bluetooth ibeacn function

Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes

第 299 场周赛 第四题 6103. 从树中删除边的最小分数

YOLOv6又快又准的目标检测框架 已开源

IDEA一键生成Log日志

浅谈GPU:历史发展,架构

快速实现Thread Mesh组网详解

Meaning of 0.0.0.0:x

Matlab GUI interface simulation DC motor and AC motor speed simulation

飞行器翼尖加速度和控制面的MPC控制
随机推荐
Fast realization of Bluetooth communication between MCU and mobile phone
内存屏障今生之Store Buffer, Invalid Queue
The song of cactus -- throwing stones to ask the way (1)
extendible hashing
vs怎么配置OpenCV?2022vs配置OpenCV详解(多图)
POI export excle
网关状态检测 echo request/reply
技术人员创业一年心得
Assembly language - Wang Shuang Chapter 11 flag register - Notes
IDEA一键生成Log日志
Tar: /usr/local: cannot find tar in the Archive: due to the previous error, it will exit in the last error state
2022 CISP-PTE(一)文件包含
AHB2APB桥接器设计(2)——同步桥设计的介绍
快速实现单片机和手机蓝牙通信
面试官:用分库分表如何做到永不迁移数据和避免热点问题?
Restrictions on the use of tidb
TiDB 基本功能
Meaning of 0.0.0.0:x
tar: /usr/local:归档中找不到tar: 由于前次错误,将以上次的错误状态退出
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT