-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKthLargestElementinBST.cpp
165 lines (156 loc) · 4.25 KB
/
KthLargestElementinBST.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Implemented by Kritagya Kumra
#include <iostream>
#include <vector>
using namespace std;
// Following is the Binary Tree node structure
class BinaryTreeNode
{
public:
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
BinaryTreeNode(int data)
{
this->data = data;
left = NULL;
right = NULL;
}
};
// void preorderTraversal(BinaryTreeNode *root, int &size)
// {
// // NLR
// if (root == NULL)
// {
// return;
// }
// size++;
// preorderTraversal(root->left, size);
// preorderTraversal(root->right, size);
// }
// int inorderTraversal1(BinaryTreeNode *root, int k, int &count, int size)
// {
// // LNR
// if (root == NULL)
// {
// return -1;
// }
// int left = inorderTraversal1(root->left, k, count, size);
// if (left != -1)
// return left;
// count++;
// if (count == size - k + 1)
// return root->data;
// return inorderTraversal1(root->right, k, count, size);
// }
// int kthLargest1(BinaryTreeNode *root, int k)
// {
// int count = 0;
// int size = 0;
// preorderTraversal(root, size);
// return inorderTraversal1(root, k, count, size);
// }
// int inorderTraversalMorrisAlgo(BinaryTreeNode *root, int k, int count)
// {
// // LNR
// if (root == NULL)
// {
// return -1;
// }
// BinaryTreeNode *curr = root;
// while (curr != NULL)
// {
// // If current->left is null then we would print current and then we would move to right
// if (curr->left == NULL)
// {
// // cout << curr->data << " ";
// count++;
// if (count == k)
// {
// return curr->data;
// }
// curr = curr->right;
// }
// // If current->left is not null then we would find its predecesssor
// else
// {
// BinaryTreeNode *predecessor = curr->left;
// while (predecessor->right != NULL && predecessor->right != curr)
// predecessor = predecessor->right;
// if (predecessor->right == NULL)
// {
// predecessor->right = curr;
// curr = curr->left;
// }
// else
// {
// predecessor->right = NULL;
// // cout << curr->data << " ";
// count++;
// if (count == k)
// {
// return curr->data;
// }
// curr = curr->right;
// }
// }
// }
// if (count < k)
// {
// return -1;
// }
// }
// int kthLargest(BinaryTreeNode *root, int k)
// {
// int count = 0;
// return inorderTraversalMorrisAlgo(root, k, count);
// }
void inorderTraversal(BinaryTreeNode *root, vector<int> &ans)
{
if (root == NULL)
{
return;
}
inorderTraversal(root->left, ans);
ans.push_back(root->data);
inorderTraversal(root->right, ans);
}
int kthLargestNew(BinaryTreeNode *root, int k)
{
vector<int> ans;
inorderTraversal(root, ans);
int size = ans.size();
int res = ans[size - k];
return res;
}
int main()
{
// node *root = NULL;
// Creation of the tree
// root = buildTree(root);
// cout << "Level Order Traversal " << endl;
// levelOrderTraversal(root);
// cout << "Reverse Level Order Traversal " << endl;
// reverseLevelOrderTraversal(root);
// cout << "Inorder Traversal " << endl;
// inorderTraversal(root);
// cout << endl;
// cout << "Preorder Traversal " << endl;
// preorderTraversal(root);
// cout << endl;
// cout << "Postorder Traversal " << endl;
// postorderTraversal(root);
// cout << endl;
// 1 3 7 -1 -1 11 -1 -1 5 17 -1 -1 -1
// buildFromLevelOrder(root);
// levelOrderTraversal(root);
BinaryTreeNode *root = new BinaryTreeNode(10);
root->left = new BinaryTreeNode(8);
root->right = new BinaryTreeNode(20);
root->left->left = new BinaryTreeNode(3);
root->left->right = new BinaryTreeNode(9);
root->right->left = new BinaryTreeNode(12);
cout << "Kth Largest element using Morris Algo" << endl;
cout << kthLargestNew(root, 3);
return 0;
}
// Implemented by Kritagya Kumra