Find K-th Smallest Element in BST

Given a binary search tree (BST) and an integer K, the task is to find the K-th smallest element in the BST.

Example:

Input:
BST: Order of elements in increasing order is { 2, 3, 4, 5, 6, 7, 8, 10 }
K = 3
Output:
4
Explanation:

The 3rd smallest element in the BST is 4.

Constraints:

  • 1 <= T <= 100
  • 1 <= N, K <= 3000
  • -10^9 <= data <= 10^9
  • Time Limit: 1 sec
Note:
The input for the tree is provided in level order, using -1 for null nodes. The task is to find and return the K-th smallest element in the format described.
AnswerBot
1d

To find the K-th smallest element in a BST, perform an in-order traversal and return the K-th element encountered.

  • Perform in-order traversal of the BST to get elements in increasing order

  • Keep track of...read more

Help your peers!
Add answer anonymously...
Goldman Sachs Software Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter