当前位置:网站首页>[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);
}
};
边栏推荐
- 【剑指offer】面试题41:数据流中的中位数——大、小堆实现
- Read the wheelevent in one article
- Troubleshooting the slow startup of spark local programs
- Interview focus - TCP protocol of transport layer
- C语言:函数栈帧
- Network principle (1) - overview of basic principles
- 【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
- 折半插入排序
- 设置提示框位置随鼠标移动,并解决提示框显示不全的问题
- Causes and solutions of deadlock in threads
猜你喜欢
随机推荐
C language: string function and memory function
Spark 3.0 DPP implementation logic
[Yunxiang book club issue 13] common methods of viewing media information and processing audio and video files in ffmpeg
UDP 的报文结构和注意事项
复杂度分析
【剑指offer】面试题49:丑数
QT (IV) mixed development using code and UI files
C语言:自定义类型
$router.back(-1)
flutter —— 布局原理与约束
js使用for in和for of来简化普通for循环
After the table is inserted into an in row formula, the cell loses focus
Insert sort directly
On juicefs
扩展Log4j支持日志文件根据时间分割文件和过期文件自动删除功能
Push down of spark filter operator on parquet file
Spark RPC
[系统编程] 进程,线程问题总结
The shell script reads the redis command in the text and inserts redis in batches
Text batch replacement function







