diff --git a/Lecture-070/4_Kth_Largest_In_BST.cpp b/Lecture-070/4_Kth_Largest_In_BST.cpp index cd9fecc..b8a6bc0 100644 --- a/Lecture-070/4_Kth_Largest_In_BST.cpp +++ b/Lecture-070/4_Kth_Largest_In_BST.cpp @@ -3,7 +3,22 @@ using namespace std; int kthLargest(Node* root, int &index, int K) { - + if(root == NULL) { + return -1; + } + + // Reverse Inorder Traversal + int right = kthLargest(root->right, index, K); + if(right != -1) { + return right; + } + + index++; + if(index == K) { + return root->data; + } + + return kthLargest(root->left, index, K); } int main() { @@ -24,4 +39,4 @@ int main() { cout << "Required answer = " << ans << endl; return 0; -} \ No newline at end of file +}