当前位置:网站首页>[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);
}
};
边栏推荐
- Network principle (2) -- network development
- 《吐血整理》C#一些常用的帮助类
- Talk about the index of interview questions
- /Dev/loop1 takes up 100% of the problem
- 面试重点——传输层的TCP协议
- HaoChen CAD building 2022 software installation package download and installation tutorial
- Database: use the where statement to retrieve (header song)
- Causes and solutions of deadlock in threads
- 使用解构交换两个变量的值
- [系统编程] 进程,线程问题总结
猜你喜欢

/Dev/loop1 takes up 100% of the problem

QT (IV) mixed development using code and UI files

The difference between synchronized and reentrantlock

UDP message structure and precautions

flutter —— 布局原理与约束

【剑指offer】面试题45:把数组排成最小的数

实现自定义Spark优化规则

Network principle (1) - overview of basic principles

IP protocol of network layer

Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
随机推荐
[正则表达式] 匹配分组
折半插入排序
js寻找数组中的最大和最小值(Math.max()方法)
C language: custom type
实现自定义Spark优化规则
多线程带来的的风险——线程安全
Spark 任务Task调度异常分析
直接插入排序
Leetcode-1: sum of two numbers
[Yunxiang book club issue 13] packaging format and coding format of audio files
[正则表达式] 匹配开头和结尾
The method of exchanging two numbers in C language
[regular expression] single character matching
传美国政府将向部分美企发放对华为销售许可证!
IP protocol of network layer
【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
网络层的IP协议
Spark3中Catalog组件设计和自定义扩展Catalog实现
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
Go language slow start -- go operator