43 / 75
Inorder traversal of BST gives sorted order; return kth element encountered.
Example:
Input:
root=[3,1,4,null,2], k=1Output:
1 (smallest element)Common Mistakes:
- Not leveraging BST property (inorder gives sorted sequence)
- Traversing entire tree instead of stopping at kth element
- Using extra space to store all values (unnecessary)
- Confusing 0-indexed vs 1-indexed k
Notes:
Inorder traversal of BST yields sorted order. Time O(h+k) best case, O(n) worst case. Space O(h). Can optimize with iterative approach and early termination.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Inorder traversal of BST gives sorted order; return kth element encountered.
Example:
Input:
root=[3,1,4,null,2], k=1Output:
1 (smallest element)Common Mistakes:
- Not leveraging BST property (inorder gives sorted sequence)
- Traversing entire tree instead of stopping at kth element
- Using extra space to store all values (unnecessary)
- Confusing 0-indexed vs 1-indexed k
Notes:
Inorder traversal of BST yields sorted order. Time O(h+k) best case, O(n) worst case. Space O(h). Can optimize with iterative approach and early termination.
43/75
Kth Smallest Element in BST