当前位置:网站首页>[Jianzhi offer] 54 The k-th node of binary search tree
[Jianzhi offer] 54 The k-th node of binary search tree
2022-07-01 13:39:00 【LuZhouShiLi】
The finger of the sword Offer 54. The second of binary search tree k Big node
subject
Given a binary search tree , Please find the first one k Large node value .
Ideas
Traverse the reverse order of the... According to the middle order of the binary search tree K Nodes
Code
/** * 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)
{
// In the sequence traversal In reverse order : Right node -> Root node -> The left node
if(!root) return ;
dfs(root->right,k);
k--;
if(!k) res = root->val; // When k be equal to 0 Description target node found There is no need to continue traversing
dfs(root->left,k);// Left
}
};
边栏推荐
- IO的几种模型 阻塞,非阻塞,io多路复用,信号驱动和异步io
- Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
- B站被骂上了热搜。。
- 单工,半双工,全双工区别以及TDD和FDD区别
- Colorful five pointed star SVG dynamic web page background JS special effect
- 7. Icons
- Flow management technology
- 焱融看 | 混合云时代下,如何制定多云策略
- 分布式事务简介(seata)
- Leetcode第一题:两数之和(3种语言)
猜你喜欢

面试题目总结(1) https中间人攻击,ConcurrentHashMap的原理 ,serialVersionUID常量,redis单线程,

Fiori 应用通过 Adaptation Project 的增强方式分享

北斗通信模块 北斗gps模块 北斗通信终端DTU

Build a vc2010 development environment and create a tutorial of "realizing Tetris game in C language"

minimum spanning tree

MySQL六十六问,两万字+五十图详解!复习必备

介绍一种对 SAP GUI 里的收藏夹事务码管理工具增强的实现方案

Spark source code (V) how does dagscheduler taskscheduler cooperate with submitting tasks, and what is the corresponding relationship between application, job, stage, taskset, and task?

Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme

学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......
随机推荐
7. Icons
龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
Simple two ball loading
2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
【机器学习】VAE变分自编码器学习笔记
Global and Chinese styrene acrylic lotion polymer development trend and prospect scale prediction report Ⓒ 2022 ~ 2028
5G工业网关的科技治超应用 超限超重超速非现场联合执法
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
微机原理与接口技术知识点整理复习–纯手打
Interpretation of R & D effectiveness measurement framework
Analysis report on the development trend and prospect scale of silicon intermediary industry in the world and China Ⓩ 2022 ~ 2027
A Fletter version of Notepad
The 14th five year plan of China's environmental protection industry and the report on the long-term goals for 2035 Ⓖ 2022 ~ 2028
arthas使用
ArrayList扩容机制以及线程安全性
Dragon lizard community open source coolbpf, BPF program development efficiency increased 100 times
Introduction to topological sorting
AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
ArrayList capacity expansion mechanism and thread safety
Nexus builds NPM dependent private database