![]() |
VOOZH | about |
Given a Binary Search Tree (BST) and a positive integer k, find the kβth smallest element in the Binary Search Tree.
Example:
Input: k = 3
π th-Largest-element-in-BSTOutput: 10
Explanation:The inorder traversal of given BST is [4, 8, 10, 12, 14, 20, 22] and its 3rd smallest element is 10.
Approach:
The idea is to perform in-order traversal using Morris Traversal Algorithm while maintaining the count of nodes visited. When count becomes equal to k, return the node value.
10
Time Complexity: O(k)
Auxiliary Space: O(1)
Related Article: