/**
* 面试题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;
}
}

最新文章

  1. react入门(3)
  2. 学习zepto.js(对象方法)[2]
  3. OpenCV从入门到放弃系列之——如何扫描图像、利用查找表和计时
  4. CCTray配置如何添加远程服务器
  5. PHP.ini文件读取不到
  6. C#设计模式——观察者模式(Observer Pattern)1
  7. 下载、运行docker
  8. xftp的使用
  9. 解决由于一个软件限制策略的阻止,windows无法运行此程序cmd.reg
  10. ios 中的构造方法
  11. AppCode3 常用 设置 及 快捷键 (持续更新)
  12. 进程与fork()、wait()、exec函数组
  13. SQL入门(3):定义约束/断言assertion/触发器trigger
  14. CentOS 7.x 安装 Docker-Compose
  15. ueditor使用
  16. Linux中通过Socket文件描述符寻找连接状态介绍
  17. 把旧系统迁移到.Net Core 2.0 日记 (19) --UI转用adminLTE
  18. Windows 10修复
  19. 华为机试-iNOC产品部-杨辉三角的变形
  20. Apache的对象复制详解

热门文章

  1. TwemProxy Redis架构
  2. python的特殊方法介绍
  3. mysql 原理 ~ 二阶段提交协议通说
  4. 【Udacity并行计算课程笔记】- Lesson 2 The GPU Hardware and Parallel Communication Patterns
  5. Apriori 算法python实现
  6. Mac环境变量配置错了以后初始化的方法
  7. linux系统切换用户
  8. elasticsearch5.0集群大数据量迁移方法及注意事项
  9. iOS 8 WKWebView 知识点
  10. OCM_第十三天课程:Section6 —》数据库性能调优 _结果缓存 /多列数据信息采集统计/采集数据信息保持游标有效