Geeksforgeeks

Timer is Running

Given a Binary search tree. Your task is to complete the function which will return the Kth largest element without doing any modification in Binary Search Tree.

**Example 1:**

**Input:
4**
/ \
** **2 9
k = 2**
Output: 4**

**Example 2:**

**Input:
** 9
\
10**
**K = 1**
Output: **10

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **kthLargest()** which takes the root of the BST and an integer K as inputs and returns the Kth largest element in the given BST.

**Expected Time Complexity:** O(H + K).

**Expected Auxiliary Space:** O(H) , where H is the height of the tree.

**Constraints:**

1 <= N <= 1000

1 <= K <= N

Author: saksham seth

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

ykaushik | 261 |

_saswata | 232 |

kirtidee18 | 1057 |

ykaushik | 733 |

aggarwalvatsal5 | 709 |

pmr_3689 | 570 |

harbilassingh | 530 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4823 |

Login to report an issue on this page.