当前位置:网站首页>leetcode - 230. The k-th smallest element in a binary search tree
leetcode - 230. The k-th smallest element in a binary search tree
2022-06-11 08:43:00 【zmm_ mohua】
leetcode - 230. Binary search tree K Small elements
subject

Code
#include <iostream>
#include <vector>
using namespace std;
typedef struct TreeNode{
int val;
struct TreeNode *left, *right;
}TreeNode, *BiTree;
void create(BiTree &root){
int tn;
cin>>tn;
if(tn == -1){
root = NULL;
return;
}
root = new TreeNode;
root->val = tn;
create(root->left);
create(root->right);
}
vector<int> nums;
void inorder(TreeNode *root){
if(!root){
return;
}
inorder(root->left);
nums.push_back(root->val);
inorder(root->right);
}
int kthSmallest(TreeNode* root, int k) {
inorder(root);
return nums[k-1];
}
int main(){
int k, res;
cin>>k;
TreeNode* root;
create(root);
res = kthSmallest(root, k);
cout<<res;
return 0;
}
边栏推荐
- The difference between & & and &
- 怎么做好项目管理?学会这4个步骤就够了
- leetcode - 230. 二叉搜索树中第K小的元素
- node报错整理
- redis6 入门级教程,有整合案例,可以直接看整合案例,简单入门,直接上手
- Zipkin入门
- Solve cannot import name 'multiheadattention' from 'tensorflow keras. layers‘
- torch. meshgrid
- GO语言:字符串的连接、数字转化字符串
- Heap can also be regarded as a tree structure. It is specified that the root node must be greater than or less than the left and right child nodes, but the size order of the left and right child nodes
猜你喜欢

CentOS essay 03:centos8.2 installing MySQL

ActiveMQ simple tutorial, suitable for beginners, learning notes yyds

指定环境下安装Jupyter
![[software tools] screen recording software captura](/img/79/705bb40fc15322243880f3b9864f2b.jpg)
[software tools] screen recording software captura

qiao-lerna:lerna辅助工具

Qiao NPMS: get the download volume of NPM packages

Matlab学习9-图像处理之非线性锐化滤波

Solve valueerror: no model found in config file

Solve cannot import name 'multiheadattention' from 'tensorflow keras. layers‘

进程间的通信
随机推荐
leetcode - 230. 二叉搜索树中第K小的元素
The role of lambdalr in pytorch
Matlab学习7-图像处理之线性平滑滤波
In place reversal of a LinkedList
Solve valueerror: no model found in config file
These gadgets are also very easy to use
Web design and website planning assignment 14 add background music to the video
Collation of open source modulation identification data set
What does it mean to buy a single-mode, dual-mode and Rechargeable Wireless Mouse
[cvpr2022] intensive reading of querydet papers
Web design and website planning assignment 13 making video playlists
for in / for of / forEach 循环
[programming development] markdown notes tutorial
SSM file upload and download
Hibernate L2 cache
(resolved) pychart debug error -unicode decodeerror: 'UTF-8' codec can't decode byte 0xe8 in position 1023
标准化编写知识
Pycharm启动卡死,loading project
Web design and website planning assignment 12 online registration form
【CVPR2022】QueryDet论文精读