63:二叉搜索树的第k个结点
2024-09-29 22:58:53
/**
* 面试题63:二叉搜索树的第k个结点
* 给定一颗二叉搜索树,请找出其中的第k大的结点
* 例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
*/
public class _63_binary_k_num {
public static void main(String[] args) {
int[] a={8,6,10,5,7,9,11};
TreeNode63 treeNode63 = new TreeNode63(8);
treeNode63.left= new TreeNode63(6);
treeNode63.right= new TreeNode63(10);
treeNode63.left.left= new TreeNode63(5);
treeNode63.left.right= new TreeNode63(7);
treeNode63.right.left= new TreeNode63(9);
treeNode63.right.right= new TreeNode63(11);
Solution63 solution63 = new Solution63();
TreeNode63 treeNode631 = solution63.KthNode(treeNode63, 3);
System.out.println(treeNode631.val);
}
}
class Solution63 {
int k;
TreeNode63 KthNode(TreeNode63 pRoot, int k) {
if(pRoot==null||k==0){
return null;
}
this.k=k;
return getKNum(pRoot);
}
public TreeNode63 getKNum(TreeNode63 pRoot){
TreeNode63 result=null;
if(pRoot!=null){
if((result=getKNum(pRoot.left))!=null){
System.out.println("上"+result.val);
return result;
}
if((k--)==1){
System.out.println("z"+pRoot.val);
return pRoot;
}
if((result=getKNum(pRoot.right))!=null){
System.out.println("x"+result.val);
return result;
}
}
return null;
}
}
class TreeNode63 {
int val = 0;
TreeNode63 left = null;
TreeNode63 right = null;
public TreeNode63(int val) {
this.val = val;
}
}
最新文章
- react入门(3)
- 学习zepto.js(对象方法)[2]
- OpenCV从入门到放弃系列之——如何扫描图像、利用查找表和计时
- CCTray配置如何添加远程服务器
- PHP.ini文件读取不到
- C#设计模式——观察者模式(Observer Pattern)1
- 下载、运行docker
- xftp的使用
- 解决由于一个软件限制策略的阻止,windows无法运行此程序cmd.reg
- ios 中的构造方法
- AppCode3 常用 设置 及 快捷键 (持续更新)
- 进程与fork()、wait()、exec函数组
- SQL入门(3):定义约束/断言assertion/触发器trigger
- CentOS 7.x 安装 Docker-Compose
- ueditor使用
- Linux中通过Socket文件描述符寻找连接状态介绍
- 把旧系统迁移到.Net Core 2.0 日记 (19) --UI转用adminLTE
- Windows 10修复
- 华为机试-iNOC产品部-杨辉三角的变形
- Apache的对象复制详解
热门文章
- TwemProxy Redis架构
- python的特殊方法介绍
- mysql 原理 ~ 二阶段提交协议通说
- 【Udacity并行计算课程笔记】- Lesson 2 The GPU Hardware and Parallel Communication Patterns
- Apriori 算法python实现
- Mac环境变量配置错了以后初始化的方法
- linux系统切换用户
- elasticsearch5.0集群大数据量迁移方法及注意事项
- iOS 8 WKWebView 知识点
- OCM_第十三天课程:Section6 —》数据库性能调优 _结果缓存 /多列数据信息采集统计/采集数据信息保持游标有效