What if you could find the 3rd smallest item instantly without sorting the whole list?
Why Kth Smallest Element in BST in DSA Javascript?
Imagine you have a big family photo album sorted by age, but it's just a messy pile of photos. You want to find the 3rd youngest family member quickly.
Without any order, you have to look at every photo and compare ages one by one.
Manually checking each photo to find the 3rd youngest is slow and confusing.
You might forget which ones you already checked or mix up the order.
This wastes time and causes mistakes.
A Binary Search Tree (BST) keeps family members sorted by age automatically.
Finding the kth smallest element means visiting nodes in order, so you can quickly find the 3rd youngest without checking all.
let ages = [45, 12, 30, 22, 10]; ages.sort((a, b) => a - b); console.log(ages[2]); // 3rd smallest by sorting whole array
function kthSmallest(root, k) {
let count = 0;
let result = null;
function inorder(node) {
if (!node || result !== null) return;
inorder(node.left);
count++;
if (count === k) result = node.val;
inorder(node.right);
}
inorder(root);
return result;
}You can quickly find the kth smallest item in a sorted structure without sorting everything again.
In a leaderboard of players sorted by score, quickly find the player with the kth highest score without scanning all players.
Manual searching is slow and error-prone.
BST keeps data sorted for fast access.
Inorder traversal helps find kth smallest efficiently.
Practice
Kth Smallest Element in BST problem ask you to find?Solution
Step 1: Understand the problem statement
The problem asks to find the element which is the Kth smallest in the BST, meaning if you list all elements in ascending order, pick the one at position K.Step 2: Clarify what Kth smallest means in BST
Since BST stores smaller values on the left and larger on the right, an inorder traversal gives sorted order. The Kth element in this order is the answer.Final Answer:
The element that is the Kth smallest value in the binary search tree -> Option AQuick Check:
Kth smallest = element at position K in sorted BST [OK]
- Confusing Kth smallest with Kth largest
- Thinking it's about tree height
- Counting nodes instead of values
root?Solution
Step 1: Check base case for recursion
function inorder(root) { if (!root) return; inorder(root.left); ... } correctly checks ifrootis null and returns to stop recursion.Step 2: Verify recursive call order
function inorder(root) { if (!root) return; inorder(root.left); ... } callsinorder(root.left)before processing current node, which is correct for inorder traversal.Final Answer:
function inorder(root) { if (!root) return; inorder(root.left); ... } -> Option CQuick Check:
Base case + left recursion = correct inorder start [OK]
- Using while loop instead of recursion
- Returning wrong subtree
- Not checking for null before recursion
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
const root = new Node(5);
root.left = new Node(3);
root.right = new Node(7);
root.left.left = new Node(2);
root.left.right = new Node(4);
let count = 0;
let result = null;
function kthSmallest(node, k) {
if (!node) return;
kthSmallest(node.left, k);
count++;
if (count === k) {
result = node.val;
return;
}
kthSmallest(node.right, k);
}
kthSmallest(root, 3);
console.log(result);Solution
Step 1: Perform inorder traversal and count nodes
The inorder traversal visits nodes in order: 2, 3, 4, 5, 7. Counting visits: 1=2, 2=3, 3=4.Step 2: Identify the 3rd visited node
The 3rd node visited is 4, soresultis set to 4.Final Answer:
4 -> Option AQuick Check:
Inorder order: 2,3,4... 3rd is 4 [OK]
- Counting before left subtree traversal
- Not stopping after finding kth element
- Confusing node values with count
function kthSmallest(root, k) {
let count = 0;
let result = null;
function inorder(node) {
if (!node) return;
inorder(node.left);
count++;
if (count === k) return node.val;
inorder(node.right);
}
result = inorder(root);
return result;
}Solution
Step 1: Analyze return values in recursion
The function returnsnode.valwhen count matches k, but does not propagate this value back up the recursive calls.Step 2: Identify missing return propagation
Without returning the found value from left or right recursive calls, the finalresultwill be undefined.Final Answer:
The recursive calls do not return the found value properly -> Option BQuick Check:
Return values must propagate up recursion [OK]
- Ignoring return values in recursion
- Not incrementing count correctly
- Missing base case check
[6, 2, 8, 0, 4, 7, 9, 3, 5], what is the 5th smallest element?Solution
Step 1: Construct BST and list inorder traversal
Inorder traversal visits nodes in ascending order: 0, 2, 3, 4, 5, 6, 7, 8, 9.Step 2: Identify the 5th element in sorted order
The 5th element is 5.Final Answer:
5 -> Option DQuick Check:
Sorted nodes: 0,2,3,4,5... 5th is 5 [OK]
- Counting nodes incorrectly
- Confusing insertion order with sorted order
- Off-by-one errors in counting
Ready to prove it?
Pass the test and this pattern moves unknown
