0
0
DSA Typescriptprogramming~10 mins

Kth Smallest Element in BST in DSA Typescript - 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 Typescript
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; return; }
    inorder(node.right);
  }
  inorder(root);
  return result;
}
This code visits nodes in ascending order and returns the kth smallest value.
Execution Table
StepOperationNode VisitedCountActionVisual State
1Start at root50Traverse left subtree5 / 3 7
2Traverse left30Traverse left subtree5 / 3 7 / 2 4
3Traverse left20Traverse left subtree5 / 3 7 / 2 4 / 1
4Visit node11Count=1, not k=3, traverse right5 / 3 7 / 2 4 / 1
5Traverse rightnull1No node, back to parent5 / 3 7 / 2 4 / 1
6Visit node22Count=2, not k=3, traverse right5 / 3 7 / 2 4 / 1
7Traverse rightnull2No node, back to parent5 / 3 7 / 2 4 / 1
8Visit node33Count=3 equals k, result=3, stop5 / 3 7 / 2 4 / 1
9Stop traversalN/A3Return result=35 / 3 7 / 2 4 / 1
💡 Count reached k=3 at node with value 3, traversal stops.
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 8Final
count01233
resultnullnullnull33
current node51233
Key Moments - 3 Insights
Why do we stop traversal immediately after count equals k?
Because once we find the kth smallest node (step 8), continuing traversal is unnecessary and inefficient, as shown in execution_table row 8.
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. This is shown in steps 2 and 3 where we go left before counting.
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. This is implied by the exit condition in the code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count after visiting node 2 (step 6)?
A1
B3
C2
D0
💡 Hint
Check the 'Count' column at step 6 in execution_table.
At which step does the traversal stop because count equals k?
AStep 4
BStep 8
CStep 6
DStep 9
💡 Hint
Look for the step where 'Count=3 equals k' in the 'Action' column.
If k was 2 instead of 3, at which node would the traversal stop?
ANode 2
BNode 1
CNode 3
DNode 4
💡 Hint
Refer to variable_tracker for count values and nodes visited in order.
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: stops early once kth found
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 left as possible, visiting nodes and counting them. When the count reaches k, we stop and return that node's value. This method is efficient because it stops early without visiting all nodes. The execution table shows each step visiting nodes 1, 2, and 3, counting them until the third node is reached, which is the kth smallest for k=3.