From a46e49bf7067859e0267db04465326c034d3c198 Mon Sep 17 00:00:00 2001 From: Hemant Kumar <123319697+hemantkumar373@users.noreply.github.com> Date: Wed, 12 Feb 2025 11:24:33 +0530 Subject: [PATCH] Update 4_Kth_Largest_In_BST.cpp --- Lecture-070/4_Kth_Largest_In_BST.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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 +}