二叉搜索树中第k大元素

Problem statement:

问题陈述:

Find the k-th smallest element in a given binary search tree (BST).

在给定的二进制搜索树(BST)中找到第k个最小的元素。

Example:

例:

    K=4
Kth smallest element in the above binary tree is: 6
K=5
Kth smallest element in the above binary tree is: 7

Solution:

解:

One possible solution is to store the in-order traversal of the BST and printing the Kth element from the list. This can be done because in-order traversal of the BST produces a sorted list. But this solution leads to the overhead of additional storage which may not be entertained.

一种可能的解决方案是存储BST的有序遍历并从列表中打印第K个元素。 之所以可以这样做是因为BST的有序遍历会生成一个排序列表。 但是该解决方案导致可能无法解决的额外存储的开销。

So, we go for a much better solution where we don’t need any additional space.

因此,我们寻求了一个更好的解决方案,不需要任何额外的空间。

Algorithm:

算法:

//k, count both parameter are passed by reference
FUNCTION kthSmallest (root, int & k, int &count) 
1.  Base case
IF (root is NULL)
Return 0;
2. Carry out in-order traversal and keep checking for kth smallest element.
left=kthSmallest (root->left, k, count) //for left subtree
IF (left)
return left;
Increment count
IF (count==k)
return root->data; //kth smallest element
kthSmallest(root->right,k,count); //for right subtree
In the main function we call kthSmallest (root, k, 0) //count 0 initially

Example with explanation:

带有说明的示例:

    In the main function we make call to kthSmallest(root, 4, 0)
-------------------------------------------------------------
KthSmallest (8, 4, 0)
8 not NULL
Left= KthSmallest(8->left , 4, 0);
Call to KthSmallest(3 , 4, 0): //8->left=3
-------------------------------------------------------------
KthSmallest (3, 4, 0)
3 not NULL
Left=KthSmallest(3->left , 4, 0);
Call to KthSmallest(1 , 4, 0): //3->left=1
-------------------------------------------------------------
KthSmallest (1, 4, 0)
1 not NULL
Left= KthSmallest(1->left , 4, 0);
Call to KthSmallest(NULL , 4, 0): //1->left=NULL
-------------------------------------------------------------
KthSmallest (NULL, 4, 0)
node is NULL
return 0;
-------------------------------------------------------------
Control back to KthSmallest (1, 4, 0):
Left=0
Increment count
Count=1;
K!=count
Call to kthSmallest(1->right, 4, 1)
-------------------------------------------------------------
This is again NULL and control backs to KthSmallest (1, 4, 0)
Since end of function reached control backs to KthSmallest (3, 4, 0)
KthSmallest (3, 4, 0):
Increment count
Count=2 //it’s not 1 because count is passed by reference
K!=count
Call to KthSmallest(3->right, 4, 2)

So if you carry out similar way you will be able to find the k-th smallest one once k==count

因此,如果执行类似的方法,则一次k == count就能找到第k个最小的 整数

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
// tree node is defined
class Node{public:
int data;
Node *left;
Node *right;
};
// creating new node
Node* newnode(int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
//finding kth smallest element
int kthSmallest(Node *root, int& k,int &count){
if(!root)
return 0;
int left=kthSmallest(root->left,k,count);
if(left)
return left;
count=count+1;
if(count==k)
return root->data;
kthSmallest(root->right,k,count);
}
int main()
{//building the bst
int count=0,k;
Node *root=newnode(8);
root->left= newnode(3);
root->right= newnode(10);
root->right->right=newnode(14);
root->right->right->left=newnode(13);
root->left->left=newnode(1);
root->left->right=newnode(6);
root->left->right->left=newnode(4);
root->left->right->right=newnode(7);
cout<<"input k\n";
cin>>k;
cout<<"Kth smallest element in the ";
cout<<"binary tree is :"<<endl;
cout<< kthSmallest(root,k,count);
return 0;
}

Output

输出量

input k
4
Kth smallest element in the binary tree is :
6

翻译自: https://www.includehelp.com/icp/k-th-smallest-element-in-a-binary-search-tree.aspx

二叉搜索树中第k大元素

二叉搜索树中第k大元素_二叉搜索树中第K个最小元素相关推荐

  1. python 无序列表中第k大元素_查询无序列表中第K小元素

    当需要在无需列表中寻找第k小的元素时,一个显然的方法是将所有数据进行排序,然后检索k个元素.这种方法的运行时间为O(n log(n)). 无序列表调用分区函数将自身分解成两个子表,其长度为i和n-i. ...

  2. 可持久化Trie+堆优化 OR Trie树上求XOR第K大 ---- P5283 [十二省联考2019]异或粽子

    题目大意 题目大意: 考虑先做个 prefix xor 前缀异或bi=⨁j=1iaj(1≤i≤n)b_i=\bigoplus_{j=1}^{i}a_j(1\leq i \leq n)bi​=j=1⨁i ...

  3. ccxt k线数据_机器学习系列:深度探秘K线结构新维度

    标星★公众号,第一时间获取最新研究 来源:光大证券金工团队独家授权 作者:胡骥聪 近期原创文章: ♥ 基于无监督学习的期权定价异常检测(代码+数据) ♥ 5种机器学习算法在预测股价的应用(代码+数据) ...

  4. java线性表删除第i个元素_第08话:线性表删除某个元素

    根据之前定义的线性表ADT,现在还剩下一个操作,就是删除了.今天把这个操作弄完. 了解线性表的插入,就很容易理解线性表的删除了.删除就是插入的逆过程. 删除算法的思路: 如果删除位置不合理,抛出异常: ...

  5. 一分钟学会看k线图_一分钟怎样学会看k线图(纯干货)散户必备!

    原标题:一分钟怎样学会看k线图(纯干货)散户必备! K线图最早是日本德川幕府时代大阪的米商用来记录当时一天.一周或一月中米价涨跌行情的图示法,后被引入股市.K线图有直观.立体感强.携带信息量大的特点, ...

  6. 手机桌面百度搜索框不显示热词_移动端搜索和PC端搜索的区别

    随着智能手机的普及,移动端搜索量是越来越大了,在去年从未有人通过移动搜索我的博客,而到了今年,从通过统计工具的数据分析来看,已经有越来越多的用户通过手机搜索到我的博客. 有一段时间移动搜索来路很大的时 ...

  7. python矩阵中找满足条件的元素_从numpy数组中取出满足条件的元素示例

    例如问题:从 arr 数组中提取所有奇数元素. input:arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) output: #> array([1, ...

  8. js remove 当前元素_详解js删除数组中的指定元素

    本篇文章将会给大家介绍两种删除数组中的指定元素的方式,分别为: 1.单独定义一个的函数,通过函数来删除指定数组元素. 2.为Array对象定义了一个removeByValue的方法,在调用方法来删除指 ...

  9. java第k大的数字,JAVA中寻找最大的K个数解法

    这个题拿到之后首先会想到排序,排好序之后在选取选取最大的K个数.排序选择快速排序是个比较好的选择. 好了,让我们来进行第一个解法:快速排序 代码如下 复制代码代码如下: public static v ...

最新文章

  1. SQL Server各种日期计算方法
  2. 第三次学JAVA再学不好就吃翔(part111)--序列流
  3. HTML5学习笔记简明版(11):新API
  4. 第一章:1.3:了解编译系统如何工作的好处
  5. python下GDAL库安装——以pycharm编译器为例
  6. 国内外手机号码正则表达式(收藏)
  7. cad2004教程_天正2014软件安装教程及下载地址
  8. 2019微信语音转发方法新版微信转发语音方法
  9. 开发方法---形式化方法
  10. Python爬虫——全网获取音乐
  11. FFmpeg开发(1)从mp4中提取aac音频
  12. 【图像处理】PS曲线工具matlab实现 交互自定义灰度映射(Gray Level Transformation)附代码链接
  13. mysql根据出生年月计算年龄
  14. python实现10进制转换2进制(两种方法)
  15. WiFi探针可以采集到哪些数据?
  16. 小时代3刺金时代好看吗?
  17. EVE模拟器使用说明
  18. APP安全测试点分析
  19. 显著性物体检测与分割
  20. CSP 202112-3 登机牌条码 40分

热门文章

  1. c语言fork()创建线程,操作系统的创建原语是fork()还是creat()?
  2. memcached 的基本命令
  3. linux系统优化篇之---top
  4. 7-深入练习Dockerfile
  5. postman里测试文件上传(MultipartFile)
  6. WPF之鼠标滑动切换图片
  7. 2015-03-19 create php alternative for myslq_result in mysqli(PHP)--PDO Tutorial for Mysql Developers
  8. 《Android应用开发攻略》——2.2 异常处理
  9. 使用visio 提示此UML形状所在的绘图页不是UML模型图的一部分 请问这个问题怎么解决?...
  10. linux 网卡驱动安装