【剑指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 ...
随机推荐
- Linux用户在第一次登录时强制更改初始密码
迫使用户更改密码 如果你想迫使用户更改其密码,请使用下面这个命令. $ sudo chage -d0 <user-name> 最初,“-d <N>”选项应该被设成密码的“有 ...
- 百度MP3音乐API接口及应用
当你在百度去搜索一首歌时,你会发现有种更简单的方法. http://box.zhangmen.baidu.com/x?op=12&count=1&title=歌名$$作者$$$$ 例如 ...
- python函数中的参数类型
python函数中的参数 动态获取函数的参数 python的函数类型详解
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- Go语言实现:【剑指offer】二叉树的镜像
该题目来源于牛客网<剑指offer>专题. 操作给定的二叉树,将其变换为源二叉树的镜像. 示例: 输入: 4 / 2 7 / \ / 1 3 6 9 输出: 4 / 7 2 / \ / 9 ...
- javascript 集合 Object Array Map Set
//Object //创建 var obj = {} function obj(){} class obj{} //Array api Array属性和方法: for 条件判断: break cont ...
- SpringBoot嵌入式Servlet配置原理
SpringBoot嵌入式Servlet配置原理 SpringBoot修改服务器配置 配置文件方式方式修改,实际修改的是ServerProperties文件中的值 server.servlet.con ...
- Transformer 和 Transformer-XL——从基础框架理解BERT与XLNet
目录写在前面1. Transformer1.1 从哪里来?1.2 有什么不同?1.2.1 Scaled Dot-Product Attention1.2.2 Multi-Head Attention1 ...
- k8s 安装ELK(6.7.0版本)和EFK
一.Elasticsearch安装 helm安装的也行,而且helm安装的stable/elasticsearch可用性更高,但是使用资源更多. 1.安装elasticsearch(线上环境千万记得把 ...
- Nginx简介入门
买了极客时间上陶辉的Nginx核心知识100讲,正在学.链接 Nginx 4个组成部分 二进制可执行文件 nginx.conf 配置文件 access.log error.log nginx 版本 M ...