Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

BST Find Minimum Element in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - BST Find Minimum Element
Start at root
↓
Check if left child exists?
No→Current node is minimum
Yes↓
Move to left child
↩Back to check left child
Start at the root node and keep moving left until no left child exists. The last node reached is the minimum.
Execution Sample
DSA Typescript
function findMin(root: TreeNode | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left) {
    current = current.left;
  }
  return current.val;
}
This code finds the smallest value in a BST by moving left until no left child exists.
Execution Table
StepOperationCurrent Node ValuePointer ChangesVisual State
1Start at root10current = root (10)10 / \ 5 15
2Check left child of current (10)10left child exists (5)10 / \ 5 15
3Move current to left child5current = 510 / \ 5 15
4Check left child of current (5)5left child exists (2)10 / \ 5 15 / 2
5Move current to left child2current = 210 / \ 5 15 / 2
6Check left child of current (2)2left child does not exist10 / \ 5 15 / 2
7Return current value as minimum2return 210 / \ 5 15 / 2
💡 Left child of current node (2) is null, so current node is minimum.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
current10 (root)522
Key Moments - 3 Insights
Why do we keep moving left to find the minimum?
In a BST, smaller values are always on the left. The execution_table rows 2, 4 show checking left child to move left until none exists.
What if the tree is empty?
The code returns null immediately (see execution_sample line 2). No steps happen in execution_table because root is null.
Why do we stop when current.left is null, not current itself?
Because the minimum is the leftmost node. If current.left is null, current is the smallest. See execution_table step 6 where left child is null.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 5?
A10
B2
C5
D15
💡 Hint
Check variable_tracker column 'After Step 5' and execution_table step 5.
At which step does the algorithm find that there is no left child?
AStep 6
BStep 4
CStep 3
DStep 7
💡 Hint
Look at execution_table step 6 where left child does not exist.
If the root had no left child, what would be the minimum value returned?
AThe right child's value
Bnull
CThe root's value
DThe smallest leaf node
💡 Hint
Refer to concept_flow and execution_table step 2 where no left child means current node is minimum.
Concept Snapshot
BST Find Minimum Element:
- Start at root node
- Move left while left child exists
- When no left child, current node is minimum
- Return current node's value
- Handles empty tree by returning null
Full Transcript
To find the minimum element in a Binary Search Tree (BST), start at the root node. Keep moving to the left child because smaller values are always on the left in a BST. When you reach a node that has no left child, that node contains the minimum value. If the tree is empty, return null immediately. This process is shown step-by-step in the execution table, where the current pointer moves from the root (10) to 5, then to 2, and stops because 2 has no left child. The minimum value found is 2.

Practice

(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At any leaf node
B. At the rightmost node
C. At the root node
D. At the leftmost node

Solution

  1. Step 1: Understand BST property

    In a BST, left child nodes have smaller values than their parents.
  2. Step 2: Locate minimum element

    The smallest value is found by going left until no more left child exists.
  3. Final Answer:

    At the leftmost node -> Option D
  4. Quick Check:

    Minimum element = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
  • Confusing minimum with root node
  • Thinking minimum is at rightmost node
  • Assuming minimum can be any leaf
2. Which TypeScript code snippet correctly finds the minimum element in a BST node?
easy
A. while (node.right !== null) { node = node.right; } return node.value;
B. while (node.left !== null) { node = node.left; } return node.value;
C. if (node.value < min) min = node.value; return min;
D. return Math.min(node.value, node.left.value);

Solution

  1. Step 1: Check traversal direction for minimum

    Minimum is found by moving left until no left child exists.
  2. Step 2: Verify code correctness

    while (node.left !== null) { node = node.left; } return node.value; correctly loops left and returns node value; others are incorrect or incomplete.
  3. Final Answer:

    while (node.left !== null) { node = node.left; } return node.value; -> Option B
  4. Quick Check:

    Traverse left to find minimum [OK]
Hint: Traverse left child nodes until null to find minimum [OK]
Common Mistakes:
  • Traversing right child instead of left
  • Using Math.min without checking null nodes
  • Not looping until leftmost node
3. What will be the output of the following TypeScript code?
class Node {
  constructor(public value: number, public left: Node | null = null, public right: Node | null = null) {}
}

const root = new Node(10, new Node(5, new Node(2), new Node(7)), new Node(15));

function findMin(node: Node): number {
  while (node.left !== null) {
    node = node.left;
  }
  return node.value;
}

console.log(findMin(root));
medium
A. 7
B. 5
C. 2
D. 10

Solution

  1. Step 1: Analyze BST structure

    Root is 10, left child 5, left-left child 2, left-right child 7, right child 15.
  2. Step 2: Trace findMin function

    Function moves left from 10 to 5, then to 2, which has no left child, so returns 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Minimum value in BST = 2 [OK]
Hint: Minimum is leftmost node value, here 2 [OK]
Common Mistakes:
  • Returning root value instead of leftmost
  • Confusing left and right child nodes
  • Stopping traversal too early
4. Identify the bug in this TypeScript function to find the minimum element in a BST:
function findMin(node: Node | null): number {
  if (node === null) return -1;
  while (node.right !== null) {
    node = node.right;
  }
  return node.value;
}
medium
A. Traverses right child instead of left child
B. Returns -1 for empty tree, should throw error
C. Does not check if node is null before accessing value
D. Returns node.value before loop ends

Solution

  1. Step 1: Check traversal direction

    Minimum element requires moving left, but code moves right.
  2. Step 2: Identify correct fix

    Change 'node.right' to 'node.left' in the while loop to find minimum.
  3. Final Answer:

    Traverses right child instead of left child -> Option A
  4. Quick Check:

    Minimum found by left traversal, not right [OK]
Hint: Minimum is leftmost, so traverse left child nodes [OK]
Common Mistakes:
  • Using right child traversal for minimum
  • Ignoring null checks before access
  • Returning default -1 instead of handling empty tree
5. Given a BST where some nodes have duplicate values, how would you modify the findMin function to return the smallest unique value?
hard
A. Traverse entire tree and collect all values, then find minimum
B. Return the first leftmost node value without checking duplicates
C. Traverse leftmost nodes and skip duplicates by checking parent values
D. Use rightmost node value as minimum to avoid duplicates

Solution

  1. Step 1: Understand duplicates in BST

    Duplicates can appear on left or right; leftmost node may not be unique minimum.
  2. Step 2: Modify approach to find unique minimum

    Traverse entire tree to collect all values, then find the smallest unique value.
  3. Final Answer:

    Traverse entire tree and collect all values, then find minimum -> Option A
  4. Quick Check:

    Full traversal needed for unique minimum [OK]
Hint: Traverse all nodes to find unique minimum value [OK]
Common Mistakes:
  • Assuming leftmost node is always unique minimum
  • Ignoring duplicates in traversal
  • Using rightmost node as minimum
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard