kth smallest element in binary search tree

Solutions on MaxInterview for kth smallest element in binary search tree by the best coders in the world

showing results for - "kth smallest element in binary search tree"
Maria José
10 Jun 2018
1#include<iostream>
2using namespace std;
3struct node
4{
5    int data;
6    node* left;
7    node* right;
8};
9int ans=0;
10node* getnode(int value)
11{
12    node* temp=new node;
13    temp->data=value;
14    temp->left=NULL;
15    temp->right=NULL;
16    return temp;
17}
18node *insert_bst(node *roots,int value)
19{
20    if(roots==NULL)
21    {
22        return getnode(value);
23    }
24    if(roots->data>value)
25    {
26        roots->left=insert_bst(roots->left,value);
27    }
28    else if(roots->data<value)
29    {
30        roots->right=insert_bst(roots->right,value);
31    }
32    return roots;
33}
34void findsol(node* roots,int *kth)
35{
36    if(roots==NULL)
37    {
38        return;
39    }
40    else
41    {
42        findsol(roots->left,kth);
43        (*kth)--;
44        if((*kth)==0)
45        {
46            ans=roots->data;
47        }
48        findsol(roots->right,kth);
49    }
50}
51int main()
52{
53    node* root=new node;
54    root=NULL;
55    int value;
56    do{
57        cin>>value;
58        if(value>0)
59        {
60            root=insert_bst(root,value);
61        }
62    }while(value>0);
63    int k;
64    cin>>k;
65    int* kth=&k;
66    findsol(root,kth);
67    cout<<ans;
68    return 0;
69}
70
queries leading to this page
return the kth smallest and largest element in bstk th smallest element in bstgiven a binary search tree find the kth smallest element practicebst find kth smallestsmallest k element in bstkth smallest element in treekth smallest element in an array using binary searchkth smallest element in a bst pythonwrite a function to find kth smallestand kth largest element in a bstk th smallest element in bstbst kth smallest element2 find k th smallest and k th largest element in bstwrite a function to find kth smallest and kth largest element in a bstkth smallest element in binary search treedetermine the kth smallest node in a bstfind the kth smallest element in a binary treekth smallest element in tree gfg practicegiven a binary search tree find the kth smallest elementkth smallest element in bstfind kth smallest element in bstkth smallest element in a bst leetcode18 kth smallest element in a bstkth smallest element in a treefind k th smallest and k th largest element in bst practicekth smallest element in bst gfg practicefind the kth smallest key in binary search treekth smallest in binary search treekt smallest element of bstkth smallest element in tree leetcodefind k th smallest in bstfind kth smallest element in bst eficient solution kth smallest element in a bst 5cfind k th smallest and k th largest element in bstkth smallest element in tree gfg kth smallest in tree bstkth smallest element in tree interviewbit solution230 kth smallest element in a bstkth smallest element in a binary search treekth smallest element in a bstkth smallest in bst pythonkth smallest in a binary search tree c 2b 2bkth smallest node treeget smallest element of binary search treegiven a binary search tree 2c write a function to find the kth smallest element in the tree kth smallest element using bstkth smallest value in a binary search treekth largest element in binary search treekth smallest element in a bst gfgkth smallest element in binary search tree