当前位置:网站首页>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;
}
边栏推荐
- Jupyter notebook code completion plug-in + Solution
- 盘它!用「飞项」轻松管理各类型项目
- Supplementary provision plan codeworks round 760 (Div. 3)
- Using flying items to manage by objectives, not being a "headless fly" in the workplace
- How to do a good job in project management? Learning these four steps is enough
- Pg/oracle database ASCII code to string custom function
- In place reversal of a LinkedList
- Pypharm startup is stuck, loading project
- torch. Var (), sample variance, parent variance
- [cvpr2022] intensive reading of querydet papers
猜你喜欢

torch. nn. functional. pad

uniapp 插件开发

Classical graph theory, depth first and breadth first, topology, prim and krukal, it's time to review

Matlab学习8-图像处理之线性与非线性锐化滤波、非线性平滑滤波

Mongodb--- automatically delete expired data using TTL index

@Usage details of postconstruct, initializingbean and initmethod

Solve valueerror: no model found in config file

Uniapp plug-in development

补2:圆环回原点问题

(resolved) typeerror: meshgrid() got an unexpected keyword argument 'indexing‘
随机推荐
剑指 Offer 10- II. 青蛙跳台阶问题
Modifying field length in Oracle and postgrepsql
How to make hyperlinks in RichTextBox- How can I make a hyperlink work in a RichTextBox?
Node error report sorting
uniapp关闭/开启/调节系统声音
CodeTop - 排序奇升偶降链表
Web design and website planning assignment 13 making video playlists
MySQL死锁问题如何解决?背诵版
torch. unbind()
torch. roll
经典图论,深度优先和广度优先,拓扑,Prim和Krukal,该来温习啦
What is concurrent search set? Are you still worried about it? In fact, it is a problem of connected graph, which is not so difficult to understand
win10家庭版如何连接远程桌面
Is the result too different from the goal? With the help of target management, you can reach the target accurately!
Zipkin入门
bat 批处理单独环境打包
leetcode - 230. 二叉搜索树中第K小的元素
【clickhouse专栏】新建库角色用户初始化
TRUNC in pytorch_ normal_ principle
Bat batch processing separate environment packaging