当前位置:网站首页>【剑指Offer】54. 二叉搜索树的第k大节点
【剑指Offer】54. 二叉搜索树的第k大节点
2022-07-01 13:26:00 【LuZhouShiLi】
剑指 Offer 54. 二叉搜索树的第k大节点
题目
给定一棵二叉搜索树,请找出其中第 k 大的节点的值。
思路
按照二叉搜索树的中序遍历倒序的第K个节点
代码
/** * 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 res;
int kthLargest(TreeNode* root, int k) {
dfs(root,k);
return res;
}
void dfs(TreeNode *root, int &k)
{
// 中序遍历 倒序:右节点->根结点->左节点
if(!root) return ;
dfs(root->right,k);
k--;
if(!k) res = root->val; // 当k等于0 说明找到目标节点 无需继续遍历
dfs(root->left,k);// 左
}
};
边栏推荐
- Reasons for MySQL reporting 1040too many connections and Solutions
- Detailed explanation of leetcode reconstruction binary tree [easy to understand]
- 龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
- SAP intelligent robot process automation (IRPA) solution sharing
- 关于佛萨奇2.0“Meta Force原力元宇宙系统开发逻辑方案(详情)
- Asp. NETCORE uses dynamic to simplify database access
- Anti fraud, refusing to gamble, safe payment | there are many online investment scams, so it's impossible to make money like this
- Global and Chinese styrene acrylic lotion polymer development trend and prospect scale prediction report Ⓒ 2022 ~ 2028
- SAP 智能机器人流程自动化(iRPA)解决方案分享
- 单工,半双工,全双工区别以及TDD和FDD区别
猜你喜欢

Interpretation of R & D effectiveness measurement framework

Understand the window query function of tdengine in one article

Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security

spark源码(五)DAGScheduler TaskScheduler如何配合提交任务,application、job、stage、taskset、task对应关系是什么?
基于mysql乐观锁实现秒杀的示例代码

龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍

Several models of IO blocking, non blocking, IO multiplexing, signal driven and asynchronous IO

当你真的学会DataBinding后,你会发现“这玩意真香”!

SAP intelligent robot process automation (IRPA) solution sharing

啟動solr報錯The stack size specified is too small,Specify at least 328k
随机推荐
Global and Chinese n-butanol acetic acid market development trend and prospect forecast report Ⓧ 2022 ~ 2028
Google Earth Engine(GEE)——全球人类居住区网格数据 1975-1990-2000-2014 (P2016)
Investment analysis and prospect prediction report of global and Chinese p-nitrotoluene industry Ⓙ 2022 ~ 2027
Wave animation color five pointed star loader loading JS special effects
Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
【机器学习】VAE变分自编码器学习笔记
MySQL报错1040Too many connections的原因以及解决方案
终端识别技术和管理技术
Interpretation of R & D effectiveness measurement framework
Computer network interview knowledge points
5. Use of ly tab plug-in of header component
Shangtang technology crash: a script written at the time of IPO
La taille de la pile spécifiée est petite, spécifiée à la sortie 328k
Declare an abstract class vehicle, which contains the private variable numofwheel and the public functions vehicle (int), horn (), setnumofwheel (int) and getnumofwheel (). Subclass mot
介绍一种对 SAP GUI 里的收藏夹事务码管理工具增强的实现方案
关于佛萨奇2.0“Meta Force原力元宇宙系统开发逻辑方案(详情)
北斗通信模块 北斗gps模块 北斗通信终端DTU
Three questions about scientific entrepreneurship: timing, pain points and important decisions
2. Sensor size "recommended collection"
Detailed explanation of leetcode reconstruction binary tree [easy to understand]