πDay 11. k-th Smallest in BST π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given a Binary Search Tree (BST) and an integer k, the task is to find the k-th smallest element in the BST.
If there is no k-th smallest element present, return -1.
π Example Walkthrough:
Example 1:
Input:
2
/ \
1 3
k = 2Output:
2Explanation:
The elements in ascending order are [1, 2, 3]. The 2nd smallest element is 2.
Example 2:
Input:
Output:
Explanation:
The BST contains only 3 elements. Hence, there is no 5th smallest element.
Example 3:
Input:
Output:
Explanation:
The in-order traversal of the BST is [4, 8, 10, 12, 14, 20, 22].
The 3rd smallest element is 10.
Constraints:
1 β€ number of nodes, k β€ $10^5$
1 β€ node->data β€ $10^5$
π― My Approach:
Morris Inorder Traversal (O(1) Space)
Morris Traversal allows in-order traversal without using a stack or recursion, thus achieving O(1) space complexity.
It temporarily modifies the tree structure but restores it before completion.
Algorithm Steps:
Start from the root node.
If the left child is
NULL:Visit the current node (decrement
k).If
k == 0, return the current node's value.Move to the right child.
If the left child exists:
Find the rightmost node in the left subtree (inorder predecessor).
If the rightmost node's right is
NULL:Make the current node its right child (create a temporary thread).
Move to the left child.
If the rightmost nodeβs right is the current node (thread exists):
Remove the thread (restore the original tree).
Visit the current node (decrement
k).If
k == 0, return the current node's value.Move to the right child.
If the traversal completes without finding the k-th smallest, return
-1.
π Time and Auxiliary Space Complexity
Expected Time Complexity:
O(N)We visit each node exactly twice (once to create a thread and once to remove it), which is linear with respect to the number of nodes.Expected Auxiliary Space Complexity:
O(1)No additional stack or recursion is used. Only a few pointers are manipulated.
π Solution Code
Code (C)
Code (C++)
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated