【剑指Offer】63、二叉搜索树的第k个结点
题目描述
给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。
题解一:DFS 借助栈实现
// 中序非递归
public static TreeNode KthNode02(TreeNode pRoot, int k) {
Stack<TreeNode> stack = new Stack<>();
if(pRoot==null||k<=0){
return null;
}
int count=0;
while(pRoot!=null||!stack.isEmpty()){
while(pRoot!=null){
stack.push(pRoot);
pRoot=pRoot.left;
}
pRoot = stack.pop();
count++;
if(count==k){
return pRoot;
}
pRoot=pRoot.right;
}
return null;
}
题解二:中序遍历
//中序遍历二叉搜索树后正好是从小到大的顺序,直接返回第k-1个即可
private static ArrayList<TreeNode> treeNode=new ArrayList<TreeNode>();
public static TreeNode KthNode01(TreeNode pRoot, int k) {
//中序遍历
InOrder(pRoot);
if(treeNode.size()<1||k<1||k>treeNode.size()) {
return null;
}
return treeNode.get(k-1);
}
public static void InOrder(TreeNode node) {
if(node!=null)
{
InOrder(node.left);
treeNode.add(node);
InOrder(node.right);
}
}
题解三:递归
public static int index = 0;
public static TreeNode KthNode(TreeNode pRoot, int k) {
//中序遍历寻找第k个
if(pRoot != null){
TreeNode node = KthNode(pRoot.left,k);
if(node != null) {
return node;
}
index ++;
if(index == k) {
return pRoot;
}
node = KthNode(pRoot.right,k);
if(node != null) {
return node;
}
}
return null;
}
初始化树:
public static class TreeNode{
int val=0;
TreeNode left=null;
TreeNode right=null;
public TreeNode(int val){
this.val=val;
}
}
private static List<TreeNode> nodeList = null;
public static TreeNode createBinTree(int[] array) {
nodeList=new LinkedList<TreeNode>();
// 将一个数组的值依次转换为TreeNode节点
for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
nodeList.add(new TreeNode(array[nodeIndex]));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
// 左孩子
nodeList.get(parentIndex).left = nodeList
.get(parentIndex * 2 + 1);
// 右孩子
nodeList.get(parentIndex).right = nodeList
.get(parentIndex * 2 + 2);
}
// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).left = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).right = nodeList
.get(lastParentIndex * 2 + 2);
}
return nodeList.get(0);
}
测试:
public static void main(String[] args) {
int[] arr= {8,6,10,5,7,9,11};
int k=2;
TreeNode root = createBinTree(arr);
TreeNode node = KthNode02(root, k);
System.out.println(node.val);
}
输出:6
【剑指Offer】63、二叉搜索树的第k个结点的更多相关文章
- 【剑指Offer】二叉搜索树的第k个结点 解题报告(Python)
[剑指Offer]二叉搜索树的第k个结点 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-intervie ...
- 剑指offer 62. 二叉搜索树的第 k 个结点
62. 二叉搜索树的第 k 个结点 题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. 法一: 非递归中序 ...
- 剑指offer:二叉搜索树的第k个结点(中序遍历)
1. 题目描述 /* 给定一棵二叉搜索树,请找出其中的第k小的结点. 例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. */ 2. 思路 中序遍历二叉搜索树,第K个就 ...
- 【Java】 剑指offer(54) 二叉搜索树的第k个结点
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 给定一棵二叉搜索树,请找出其中的第k小的结点. 思路 设置全局变量 ...
- 剑指Offer 62. 二叉搜索树的第k个结点 (二叉搜索树)
题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. 例如, 5 / \ 3 7 / \ / \ 2 4 6 ...
- [剑指Offer] 62.二叉搜索树的第k个结点
题目描述 给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. [思路]遍历二叉搜索树,存入一个vector ...
- 力扣 - 剑指 Offer 54. 二叉搜索树的第k大节点
题目 剑指 Offer 54. 二叉搜索树的第k大节点 思路1 二叉搜索树的特性就是中序遍历结果为递增序列,而题目要求的是第 k 大节点,所以就应该是要遍历结果为降序, 按照先遍历左子树.输出节点.遍 ...
- Go语言实现:【剑指offer】二叉搜索树的第k个的结点
该题目来源于牛客网<剑指offer>专题. 给定一棵二叉搜索树,请找出其中的第k小的结点.例如,(5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. Go语言实现: ...
- 剑指offer——59二叉搜索树的第k大节点
题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. 题解: 考察的就是中序遍历 不过注意进行剪枝 cl ...
- 每日一题 - 剑指 Offer 54. 二叉搜索树的第k大节点
题目信息 时间: 2019-07-04 题目链接:Leetcode tag:二叉搜索树 中序遍历 递归 难易程度:中等 题目描述: 给定一棵二叉搜索树,请找出其中第k大的节点. 示例1: 输入: ro ...
随机推荐
- Qt qApp
qApp A global pointer referring to the unique application object. It is equivalent to the pointer re ...
- windows7_下Eclipse中部署tomcat7.0进行JSP+servlet开发
环境:windows 7+EclipseJava EE IDE for Web Developers +tomcat 7.02 插件:tomcatPluginV321.zip(百度搜索下载即可) 一. ...
- mod3 如何用硬件实现
今天接到Qualcom 的电话面试,表现很一般.Qualcom 不愧是一流的IC 设计公司,问得问题非常基础,但是非常深入! 其中问了一个如何实现模3 的问题.没有回答上来. 后来想了一下,其实非常简 ...
- ELK同步kafka带有key的Message
需求 kafka中的message带有key,带有相同key值的message后入kafka的意味着更新message,message值为null则意味着删除message. 用logstash来同步 ...
- Codeforces_820
A.直接模拟. #include<bits/stdc++.h> using namespace std; int c,v0,v1,a,l; int main() { ios::sync_w ...
- [redis读书笔记] 第一部分 数据结构与对象 压缩列表
压缩列表是为了节省内存而设计的,是列表键和哈希键的底层实现之一. 压缩列表的逻辑如下,
- Keepalived 配置文件
keepalived的配置文件: keepalived只有一个配置文件keepalived.conf,里面主要包括以下几个配置区域,分别是global_defs. 全局定义及 ...
- VMware vCenter Server6.5安装及群集配置介绍
借助 VMware vCenterServer,可从单个控制台统一管理数据中心的所有主机和虚拟机,该控制台聚合了集群.主机和虚拟机的性能监控功能. VMware vCenterServer 使管理员能 ...
- MarkdownPad2 安装以及出现的错误(This view has crashed)
在这里首先感谢 堃堃5love 的解决办法 原文链接:https://blog.csdn.net/kunkun5love/article/details/79495618 声明:写这个是为了以后遇见问 ...
- C# 获取键盘钩子,屏蔽键盘按键
static int hHook = 0; public delegate int HookProc(int nCode, int wParam, IntPtr lParam); //LowLevel ...