-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkth_largest_or_smallest_element.cpp
87 lines (77 loc) · 1.76 KB
/
kth_largest_or_smallest_element.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <vector>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int val)
{
this->data = val;
left = NULL;
right = NULL;
}
};
/* concept -> if the root is null then simply return the NULL.
// now go to the left part.
// if the left part is returning the NULL, then don't do anything. but if the left part is not null
// then return the left part.
Time complexity -> O(Nodes)
space complexity -> O(space taken by recusive stack[Height of tree])
*/
Node *inorder(Node *root, int k, int &count)
{
if (root == NULL)
return NULL;
// left part.
Node *left = inorder(root->left, k, count);
if (left != NULL)
{
cout << "i am in the left part if statement." << endl;
return left;
}
// processing the node.
count++;
if (count == k)
{
cout << "root -> " << root->data << endl;
cout << "count -> " << count << endl;
return root;
}
// right part.
return inorder(root->right, k, count);
}
int kthSmallest(Node *root, int k)
{
// traverse the tree in inorder fashion.
int count = 0;
Node *node = inorder(root, k, count);
if (node)
return node->data;
return -1;
}
/* time complexiy -> O(Nodes)
space complexity -> O(H) [for recursive call stack] + O(Nodes) [for vector.]
void inorder(Node* root, vector<int> &store)
{
if(root == NULL){
return;
}
inorder(root->left,store);
store.push_back(root->data);
inorder(root->right,store);
}
int kthSmallest(Node* root, int k) {
vector<int>store;
inorder(root, store);
if(store.size() < k)
return -1;
return store[k-1];
}
*/
int main()
{
return 0;
}