Discover how to find the kth smallest item instantly without sorting everything!
Why Kth Smallest Element in BST in DSA C?
Imagine you have a big family photo album sorted by age, but you want to find the 5th youngest person. Without a system, you'd have to look at every photo one by one, counting each person until you reach the 5th. This is slow and tiring.
Manually checking each photo or number means you waste time and can easily lose count or make mistakes. If the album is huge, this becomes frustrating and error-prone.
Using a special tree that keeps things sorted, called a Binary Search Tree (BST), you can quickly jump to the 5th smallest item by smartly moving left or right. This saves time and avoids mistakes.
int findKthSmallest(int arr[], int size, int k) {
sort(arr, arr + size);
return arr[k-1];
}int kthSmallest(struct Node* root, int* k) {
if (!root) return -1;
int left = kthSmallest(root->left, k);
if (left != -1) return left;
(*k)--;
if (*k == 0) return root->data;
return kthSmallest(root->right, k);
}This lets you find the kth smallest item in a sorted structure quickly without checking every element.
Finding the kth fastest runner in a race results list without sorting the entire list again.
Manual counting is slow and error-prone.
BST structure keeps data sorted for quick access.
Recursive search finds kth smallest efficiently.
Practice
kth smallest element in a BST mean?Solution
Step 1: Understand BST property
A Binary Search Tree (BST) stores elements so that left subtree has smaller values and right subtree has larger values.Step 2: Define kth smallest element
The kth smallest element is the one that appears in the kth position when all elements are sorted in ascending order.Final Answer:
The element that appears in the kth position when BST elements are sorted in ascending order -> Option AQuick Check:
kth smallest = kth in sorted order [OK]
- Confusing kth smallest with kth largest
- Thinking kth smallest means kth level node
- Assuming frequency matters
struct Node?Solution
Step 1: Understand function parameters
The function needs the root pointer to the BST and the integer k to find kth smallest.Step 2: Check parameter types
Passingstruct Node*for root andintfor k is correct. Return type should beintfor the element value.Final Answer:
int kthSmallest(struct Node* root, int k); -> Option CQuick Check:
Correct signature includes pointer and k parameter [OK]
- Passing root by value instead of pointer
- Missing k parameter
- Wrong return type or missing return
k = 3, what is the output of the function kthSmallest(root, 3)?
5
/ \
3 7
/ \ \
2 4 8
Solution
Step 1: Perform inorder traversal
Inorder traversal of BST visits nodes in ascending order: 2, 3, 4, 5, 7, 8.Step 2: Identify 3rd element in traversal
The 3rd element is 4.Final Answer:
4 -> Option BQuick Check:
Inorder 3rd element = 4 [OK]
- Counting nodes incorrectly in traversal
- Confusing preorder or postorder with inorder
- Choosing root or other node instead of kth
int count = 0;
int kthSmallest(struct Node* root, int k) {
if (!root) return -1;
int left = kthSmallest(root->left, k);
if (left != -1) return left;
count++;
if (count == k) return root->data;
return kthSmallest(root->right, k);
}
What is the main issue with this code?Solution
Step 1: Analyze use of 'count'
The variable 'count' is global and not reset between calls, causing wrong counts if function called multiple times.Step 2: Understand impact on results
Because 'count' is not reset, subsequent calls may return wrong kth smallest element or fail.Final Answer:
Global variable 'count' causes incorrect results on multiple calls -> Option AQuick Check:
Global count not reset breaks repeated calls [OK]
- Ignoring global variable side effects
- Assuming function works for multiple calls
- Not resetting count before each call
Solution
Step 1: Understand duplicates in BST
Duplicates appear as multiple nodes with same value; inorder traversal visits them in sorted order including duplicates.Step 2: Modify traversal to skip duplicates
Track previous visited value during inorder traversal; only increment count when current value differs from previous.Step 3: Find kth unique element
Increment count only for unique values; when count equals k, return current node's value.Final Answer:
Use inorder traversal and skip duplicates by comparing with previous visited value -> Option DQuick Check:
Skip duplicates by comparing previous value [OK]
- Counting duplicates as separate elements
- Using preorder traversal which is not sorted
- Trying to remove duplicates instead of skipping
Ready to prove it?
Pass the test and this pattern moves unknown
