当前位置:网站首页>【LeetCode 剑指 Offer 54. 二叉搜索树的第k大节点(简单)】
【LeetCode 剑指 Offer 54. 二叉搜索树的第k大节点(简单)】
2022-04-22 23:10:00 【Minaldo7】
题目:
给定一棵二叉搜索树,请找出其中第 k 大的节点的值。

限制:
1 ≤ k ≤ 二叉搜索树元素个数
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题过程①:
中序遍历
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
List<Integer> list = new LinkedList<>();
public int kthLargest(TreeNode root, int k) {
PreOrder(root,k);
return list.get(list.size()-k);
}
private void PreOrder(TreeNode root,int k){
if(root==null) return;
PreOrder(root.left, k);
list.add(root.val);
PreOrder(root.right, k);
}
}
执行结果①:

更高级的版本
解题过程②:
中序遍历,先右,再中,后左。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) {
val = x; }
* }
*/
class Solution {
int count=0, ans=0;
public int kthLargest(TreeNode root, int k) {
InOrder(root, k);
return ans;
}
private void InOrder(TreeNode root,int k){
if(root==null) return;
// 先右
InOrder(root.right, k);
// 再中
if(++count==k){
ans = root.val;
return;
}
// 后左
InOrder(root.left, k);
}
}
执行结果②:

版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Su_Del/article/details/124355688
边栏推荐
- 华为机试题——HJ76 尼科彻斯定理
- [BJDCTF2020]Easy MD5
- [swift] code triggers uibutton click event
- L1-049 ladder race seat allocation (20 points) (in-depth understanding of for loop + three-dimensional array + error analysis)
- STM32 内存分配解析及变量的存储位置
- 正则表达式——IP地址匹配
- MySQL最新教程通俗易懂
- vtkVertex 顶点
- Cron expression
- Unity uses newtonsoft JSON plug-in realizes the conversion of XML and JSON data
猜你喜欢

如何实现根据照片获取地理位置及如何防御照片泄漏地理位置

Future source code | Professor Wu Enda's lecture: Tips for using a data centric AI approach

【Vmware】Vmware ESXI 6.7 安装

visio文本框输入公式

Canal使用流程、部署安装文档

SystemVerilog 验证-测试平台编写指南学习笔记(5):功能覆盖率

Enter a formula in the Visio text box
![[original] [open source] C WinForm DPI adaptive scheme, sunnyui three-step solution](/img/de/145c3b65d412b863cd845bb861003b.png)
[original] [open source] C WinForm DPI adaptive scheme, sunnyui three-step solution

Redis deployment

Excel VBA 多条件筛选及汇部统计
随机推荐
SQL语言详解
DEJA_VU3D - Cesium功能集 之 014-剖面分析
visio文本框输入公式
CSV Column Extract列提取
【毕业设计】答 辩 技 巧 一(以一个过来人的身份,祝各位答辩 过 过 过)
LeetCode 16. 最接近的三数之和(中等、数组)day13
ASP connect MSSQL database statement
想开户交易农产品期货如玉米、豆粕等,该如何开户?
Basic use and principle of Minio
High number | [differential calculus and application of multivariate functions] error prone problems and detailed explanation of Li Lin 880
How to open MT5 account and what products can MT5 trade?
MySQL日志保留策略:设置binlog日志保存天数、文件大小限制
Pytorch 卷积核填充和步幅、多输入多输出通道、池化层
不谈赛道,不聊风口,开源数据库巨头Cassandra如何在国内讲好“新故事”
how to become professional
Ignition Modbus
L1-049 ladder race seat allocation (20 points) (in-depth understanding of for loop + three-dimensional array + error analysis)
Future source code | Professor Wu Enda's lecture: Tips for using a data centric AI approach
How to modify the QR code style when scanning the code and logging in the background management system of enterprise wechat
GBase 8a常用并行度参数调整提升性能