Bird
Raised Fist0
DSA C#programming

BST Find Minimum Element in DSA C#

Choose your learning style10 modes available
Mental Model
In a binary search tree, the smallest value is always found by going as far left as possible.
Analogy: Imagine a tree where smaller numbers are always on the left side. To find the smallest number, you just keep walking left until you can't go further.
      10
     /  \
    5    15
   / \     \
  2   7     20
 ↑
(root)
Dry Run Walkthrough
Input: BST: 10 -> 5 -> 15 -> 2 -> 7 -> 20; find minimum element
Goal: Find the smallest value in the BST by moving left until no more left child exists
Step 1: Start at root node with value 10
10 -> 5 -> 15 -> 2 -> 7 -> 20
↑ (at 10)
Why: We begin searching from the root to find the minimum
Step 2: Move left from 10 to node 5
10 -> [curr->5] -> 15 -> 2 -> 7 -> 20
Why: Left child is smaller, so we move left to find smaller values
Step 3: Move left from 5 to node 2
10 -> 5 -> 15 -> [curr->2] -> 7 -> 20
Why: Keep moving left to find the smallest value
Step 4: Node 2 has no left child, stop here
10 -> 5 -> 15 -> [curr->2] -> 7 -> 20
Why: No more left nodes means current node is the minimum
Result:
Minimum element found: 2
Annotated Code
DSA C#
using System;

public class Node
{
    public int Value;
    public Node Left, Right;

    public Node(int value)
    {
        Value = value;
        Left = Right = null;
    }
}

public class BST
{
    public Node Root;

    public BST()
    {
        Root = null;
    }

    public void Insert(int value)
    {
        Root = InsertRec(Root, value);
    }

    private Node InsertRec(Node root, int value)
    {
        if (root == null)
            return new Node(value);

        if (value < root.Value)
            root.Left = InsertRec(root.Left, value);
        else
            root.Right = InsertRec(root.Right, value);

        return root;
    }

    public int FindMin()
    {
        if (Root == null)
            throw new InvalidOperationException("BST is empty");

        Node current = Root;
        while (current.Left != null) // move left until no left child
        {
            current = current.Left; // advance current to left child
        }
        return current.Value; // current is minimum
    }
}

class Program
{
    static void Main()
    {
        BST tree = new BST();
        int[] values = {10, 5, 15, 2, 7, 20};
        foreach (int val in values)
            tree.Insert(val);

        int min = tree.FindMin();
        Console.WriteLine(min);
    }
}
while (current.Left != null)
keep moving left to find smaller values
current = current.Left;
advance current pointer to left child
return current.Value;
return the smallest value found
OutputSuccess
2
Complexity Analysis
Time: O(h) where h is the height of the tree, because we only move down the left side
Space: O(1) because we use only a few variables, no extra data structures
vs Alternative: Compared to scanning all nodes (O(n)), this is faster because BST structure guides us directly to the minimum
Edge Cases
Empty BST
Throws an exception because there is no minimum
DSA C#
if (Root == null) throw new InvalidOperationException("BST is empty");
BST with only one node
Returns that single node's value as minimum
DSA C#
while (current.Left != null)
When to Use This Pattern
When you need the smallest element in a BST, look for the leftmost node because BSTs keep smaller values on the left.
Common Mistakes
Mistake: Trying to find minimum by checking all nodes instead of just going left
Fix: Use a loop to move left until no left child exists
Mistake: Not handling empty tree before searching minimum
Fix: Add a check for empty root and handle it properly
Summary
Finds the smallest value in a BST by moving left until no more left child exists.
Use when you want the minimum element quickly in a BST.
The smallest value is always the leftmost node in the tree.

Practice

(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At the rightmost node
B. At the root node
C. At the leftmost node
D. At any leaf 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 as far left as possible from the root.
  3. Final Answer:

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

    Minimum element = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
  • Thinking minimum is at root always
  • Confusing leftmost with rightmost node
  • Assuming minimum can be any leaf
2. Which of the following C# code snippets correctly finds the minimum element in a BST node named root?
easy
A. while (root.Left != null) { root = root.Left; } return root.Value;
B. while (root.Right != null) { root = root.Right; } return root.Value;
C. return root.Value;
D. while (root != null) { root = root.Left; } return root.Value;

Solution

  1. Step 1: Check loop condition for minimum

    Minimum is found by moving left while left child exists.
  2. Step 2: Validate code correctness

    while (root.Left != null) { root = root.Left; } return root.Value; moves left until null, then returns last node's value correctly.
  3. Final Answer:

    while (root.Left != null) { root = root.Left; } return root.Value; -> Option A
  4. Quick Check:

    Traverse left until null, then return value [OK]
Hint: Traverse left child until null to find minimum [OK]
Common Mistakes:
  • Traversing right child instead of left
  • Returning root value without traversal
  • Looping until root is null causing error
3. What will be the output of the following C# code snippet?
class Node {
  public int Value;
  public Node Left, Right;
  public Node(int val) { Value = val; }
}

Node root = new Node(10);
root.Left = new Node(5);
root.Right = new Node(15);
root.Left.Left = new Node(2);
root.Left.Right = new Node(7);

Node current = root;
while (current.Left != null) {
  current = current.Left;
}
Console.WriteLine(current.Value);
medium
A. 7
B. 2
C. 5
D. 10

Solution

  1. Step 1: Trace the tree structure

    Root is 10, left child 5, left-left child 2, left-right child 7.
  2. Step 2: Follow the loop to find minimum

    Loop moves from 10 to 5, then to 2 (leftmost node). 2 has no left child, loop stops.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Leftmost node value = 2 [OK]
Hint: Leftmost node value is printed after loop [OK]
Common Mistakes:
  • Printing root value instead of leftmost
  • Confusing left-right child as minimum
  • Stopping loop too early
4. Identify the error in the following C# method to find the minimum element in a BST:
int FindMin(Node root) {
  while (root != null) {
    root = root.Left;
  }
  return root.Value;
}
medium
A. The method should check root.Right instead of root.Left
B. The method should return root.Left.Value instead of root.Value
C. The method should use recursion instead of a loop
D. The loop condition should be root.Left != null, not root != null

Solution

  1. Step 1: Analyze loop condition

    Loop runs while root is not null, but root becomes null after last left move.
  2. Step 2: Identify cause of error

    After loop, root is null, so accessing root.Value causes error. Condition should be root.Left != null to stop at last node.
  3. Final Answer:

    The loop condition should be root.Left != null, not root != null -> Option D
  4. Quick Check:

    Stop before null to access valid node [OK]
Hint: Loop until root.Left is null, not root itself [OK]
Common Mistakes:
  • Looping until root is null causing null reference
  • Returning value after root becomes null
  • Checking right child instead of left
5. Given a BST where some nodes have duplicate values, which approach correctly finds the minimum element considering duplicates are allowed only on the right subtree?
class Node {
  public int Value;
  public Node Left, Right;
  public Node(int val) { Value = val; }
}

// Duplicates go to right subtree
Which code snippet correctly finds the minimum element?
hard
A. while (root.Left != null) { root = root.Left; } return root.Value;
B. while (root.Right != null) { root = root.Right; } return root.Value;
C. return root.Value; // root is minimum due to duplicates on right
D. while (root.Left != null && root.Left.Value < root.Value) { root = root.Left; } return root.Value;

Solution

  1. Step 1: Understand duplicate placement rule

    Duplicates go to right subtree, so minimum is still in leftmost path.
  2. Step 2: Confirm traversal for minimum

    Traverse left child nodes until no more left child exists to find minimum.
  3. Final Answer:

    while (root.Left != null) { root = root.Left; } return root.Value; -> Option A
  4. Quick Check:

    Minimum is leftmost node even with duplicates on right [OK]
Hint: Duplicates on right don't affect leftmost minimum [OK]
Common Mistakes:
  • Checking right subtree for minimum
  • Assuming root is minimum without traversal
  • Adding extra conditions unnecessarily
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