二叉搜索树中第k大元素_二叉搜索树中第K个最小元素
二叉搜索樹中第k大元素
Problem statement:
問題陳述:
Find the k-th smallest element in a given binary search tree (BST).
在給定的二進制搜索樹(BST)中找到第k個最小的元素。
Example:
例:
K=4Kth smallest element in the above binary tree is: 6K=5Kth smallest element in the above binary tree is: 7Solution:
解:
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 caseIF (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 subtreeIF (left)return left;Increment countIF (count==k)return root->data; //kth smallest elementkthSmallest(root->right,k,count); //for right subtreeIn the main function we call kthSmallest (root, k, 0) //count 0 initiallyExample with explanation:
帶有說明的示例:
In the main function we make call to kthSmallest(root, 4, 0)-------------------------------------------------------------KthSmallest (8, 4, 0)8 not NULLLeft= KthSmallest(8->left , 4, 0);Call to KthSmallest(3 , 4, 0): //8->left=3-------------------------------------------------------------KthSmallest (3, 4, 0)3 not NULLLeft=KthSmallest(3->left , 4, 0);Call to KthSmallest(1 , 4, 0): //3->left=1-------------------------------------------------------------KthSmallest (1, 4, 0)1 not NULLLeft= KthSmallest(1->left , 4, 0);Call to KthSmallest(NULL , 4, 0): //1->left=NULL-------------------------------------------------------------KthSmallest (NULL, 4, 0)node is NULLreturn 0;-------------------------------------------------------------Control back to KthSmallest (1, 4, 0):Left=0Increment countCount=1;K!=countCall 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 countCount=2 //it’s not 1 because count is passed by referenceK!=countCall 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 bstint 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个最小元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: des加密密码补位_密码学中的数据加密标
- 下一篇: Python 爬取淘宝商品信息栏目