当前位置:网站首页>[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
}
};
边栏推荐
- SAP intelligent robot process automation (IRPA) solution sharing
- Sign APK with command line
- 3.4 data query in introduction to database system - select (single table query, connection query, nested query, set query, multi table query)
- Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
- When you really learn databinding, you will find "this thing is really fragrant"!
- Kongsong (Xintong Institute) - cloud security capacity building and trend in the digital era
- Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
- Global and Chinese polypropylene industry prospect analysis and market demand forecast report Ⓝ 2022 ~ 2027
- [241. Design priority for operation expression]
- Reasons for MySQL reporting 1040too many connections and Solutions
猜你喜欢

Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,

Application of 5g industrial gateway in scientific and technological overload control; off-site joint law enforcement for over limit, overweight and overspeed

minimum spanning tree
Example code of second kill based on MySQL optimistic lock

流量管理技术

Flow management technology

啟動solr報錯The stack size specified is too small,Specify at least 328k

Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO

1553B environment construction

Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme
随机推荐
Research Report on China's software outsourcing industry investment strategy and the 14th five year plan Ⓡ 2022 ~ 2028
Meta enlarge again! VR new model posted on CVPR oral: read and understand voice like a human
04 redis source code data structure dictionary
1553B环境搭建
用命令行 给 apk 签名
Interpretation of R & D effectiveness measurement framework
【机器学习】VAE变分自编码器学习笔记
Asp. NETCORE uses dynamic to simplify database access
8款最佳实践,保护你的 IaC 安全!
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
word2vec训练中文词向量
Analysis report on production and marketing demand and investment forecast of global and Chinese diamond powder industry Ⓤ 2022 ~ 2027
Leetcode第一题:两数之和(3种语言)
1. Sum of two numbers: given an integer array num and an integer target value, please find the two integers whose sum is the target value target in the array and return their array subscripts
【241. 为运算表达式设计优先级】
How much money do novices prepare to play futures? Is agricultural products OK?
MySQL六十六问,两万字+五十图详解!复习必备
Apache-Atlas-2.2.0 独立编译部署
Camp division of common PLC programming software
China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028