0
0
DSA Javascriptprogramming~10 mins

Kth Smallest Element in BST in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Kth Smallest Element in BST
Start at root node
Traverse left subtree
Visit current node
Count visited nodes
Is count == k?
NoTraverse right subtree
Yes
Return current node value
Done
We go left as far as possible, visit nodes in order, count them, and stop when we reach the kth node.
Execution Sample
DSA Javascript
function kthSmallest(root, k) {
  let count = 0, result = null;
  function inorder(node) {
    if (!node || result !== null) return;
    inorder(node.left);
    count++;
    if (count === k) { result = node.val; return; }
    inorder(node.right);
  }
  inorder(root);
  return result;
}
This code finds the kth smallest value in a BST by visiting nodes in ascending order.
Execution Table
StepOperationCurrent NodeCountResultVisual State
1Start at root50null5 / \ 3 7 / \ 2 4 8
2Traverse left30null5 / \ 3 7 / \ 2 4 8
3Traverse left20null5 / \ 3 7 / \ 2 4 8
4Visit node21null5 / \ 3 7 / \ 2 4 8
5Traverse right (null)null1null5 / \ 3 7 / \ 2 4 8
6Back to node31null5 / \ 3 7 / \ 2 4 8
7Visit node32null5 / \ 3 7 / \ 2 4 8
8Traverse right42null5 / \ 3 7 / \ 2 4 8
9Visit node4345 / \ 3 7 / \ 2 4 8
10Stop traversalnull345 / \ 3 7 / \ 2 4 8
💡 Traversal stops when count reaches k=3 at node with value 4.
Variable Tracker
VariableStartAfter Step 4After Step 7After Step 9Final
count01233
resultnullnullnull44
current node5234null
Key Moments - 3 Insights
Why do we stop traversal immediately after finding the kth smallest element?
Because once count equals k (see step 9 in execution_table), we have found the kth smallest node, so continuing traversal is unnecessary and avoided by checking result !== null.
Why do we traverse left subtree first before visiting the current node?
In a BST, left subtree nodes are smaller, so inorder traversal visits nodes in ascending order (see steps 2 and 3 before visiting nodes).
What happens if the BST has fewer than k nodes?
The traversal finishes without count reaching k, so result remains null, indicating kth smallest does not exist (not shown in this example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count at step 7?
A2
B1
C3
D0
💡 Hint
Check the 'Count' column in execution_table row for step 7.
At which step does the traversal stop because the kth smallest element is found?
AStep 4
BStep 7
CStep 9
DStep 10
💡 Hint
Look for when 'Result' changes from null to a value in execution_table.
If k was 2 instead of 3, what would be the result at the end?
A2
B3
C4
Dnull
💡 Hint
Check variable_tracker for count and result values at steps before step 9.
Concept Snapshot
Kth Smallest Element in BST:
- Use inorder traversal (left, node, right)
- Count nodes visited in ascending order
- Stop when count == k
- Return current node's value
- Efficient for BST due to sorted order
Full Transcript
To find the kth smallest element in a Binary Search Tree, we use inorder traversal which visits nodes in ascending order. We start at the root and go as far left as possible, visiting nodes and counting them. When the count reaches k, we stop and return that node's value. This method leverages the BST property that left children are smaller and right children are larger. The traversal stops early once the kth element is found to save time. If the tree has fewer than k nodes, the result is null.