当前位置:网站首页>[sword finger offer] interview question 54: the k-largest node of the binary search tree
[sword finger offer] interview question 54: the k-largest node of the binary search tree
2022-07-27 15:49:00 【Jocelin47】

Method 1 : In the sequence traversal + Index count return
Because the binary search tree given by the topic
Therefore, you can traverse from large to small through the medium order traversal
But you need to traverse the right subtree first and then the left subtree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
class Solution {
public:
int index = 0;
int k_value = 0;
int kthLargest(TreeNode* root, int k) {
preOrder(root, k);
return k_value;
}
void preOrder(TreeNode* root,int k)
{
if( root == nullptr )
return;
preOrder(root->right,k);
index++;
if(index == k)
{
k_value = root->val;
return;
}
preOrder(root->left,k);
}
};
边栏推荐
- Spark 任务Task调度异常分析
- C语言中交换两数的方法
- 直接插入排序
- 聊聊面试必问的索引
- go语言慢速入门——包
- The difference between synchronized and reentrantlock
- [Yunxiang book club issue 13] common methods of viewing media information and processing audio and video files in ffmpeg
- Spark 3.0 DPP实现逻辑
- 【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
- 股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
猜你喜欢
随机推荐
JS uses unary operators to simplify string to number conversion
【剑指offer】面试题49:丑数
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
Troubleshooting the slow startup of spark local programs
Spark 本地程序启动缓慢问题排查
Implementation of spark lazy list files
The shell script reads the redis command in the text and inserts redis in batches
/Dev/loop1 takes up 100% of the problem
leetcode-1:两数之和
The difference between synchronized and reentrantlock
数组名是首元素地址吗?
C语言:动态内存函数
Hj8 consolidated statement record
Talk about the index of interview questions
Implement custom spark optimization rules
Spark 3.0 测试与使用
Spark 3.0 testing and use
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
Go language slow start - Basic built-in types




![[tensorboard] oserror: [errno 22] invalid argument processing](/img/bf/c995f487607e3b307a268779ec1e94.png)




