Find the Minimum Value in a BST - Algorithm, Visualization, Examples

Visualization Player

Problem Statement

Given a Binary Search Tree (BST), find the minimum value stored in the tree. In a BST, the left child of a node contains a value less than the node itself, and the right child contains a value greater. You need to return the smallest value among all the nodes. If the tree is empty, return null or an appropriate error/indicator.

Examples

Input Tree Minimum Value Description
[10, 5, 15, 2, 7, null, 20]
2 The minimum value is at the leftmost node of the BST: 2.
[25, 20, 36, 10, 22, 30, 40]
10 BST's minimum is the leftmost node from the root: 10.
[50, 30, 70, 20, 40, 60, 80]
20 Smallest element in this BST is at the far left leaf: 20.
[8, null, 10, null, 12]
8 There is no left subtree, so the root itself is the minimum.
[100]
100 Single node tree, so root is also the minimum.
[] None Empty tree has no nodes; hence, no minimum exists.

Solution

Understanding the Problem

We are given a Binary Search Tree (BST), and we are asked to find the minimum value present in it. A Binary Search Tree is a binary tree where for each node: - All values in the left subtree are less than the node. - All values in the right subtree are greater than the node.

The minimum value in a BST will always be the leftmost node because smaller values are always inserted to the left. Let's understand this with a clear example and then walk through the solution step by step.

Step-by-Step Solution with Example

Step 1: Look at the Root

Start with the root node of the BST. This is where our search for the minimum value begins. Let's consider the following example tree:

    10
   /    5    15
 /
2

The root node here is 10.

Step 2: Move to the Left Child

According to BST rules, the left child will always have a smaller value. So from 10, we move left to 5.

Step 3: Keep Going Left

From 5, we again move left to 2, because we are looking for the smallest value. If 2 had a left child, we would continue left. But 2 has no left child.

Step 4: Stop at the Leftmost Node

Since 2 has no further left children, it is the minimum value in this BST. This logic applies to all BSTs — the leftmost node is always the smallest.

Edge Cases

Case 1: Left-skewed Tree

    20
   /
  10
 /
5
/
3

In this case, we keep moving left from 20 → 10 → 5 → 3. The node 3 has no left child, so it is the minimum value.

Case 2: Single-node Tree

    42

There are no left or right children. The root itself is the minimum value.

Case 3: Empty Tree

If the BST is empty (i.e., root is null), there is no minimum value. Your function should handle this case safely by either:

  • Returning null or None,
  • Throwing an appropriate exception, or
  • Returning a message that the tree is empty.

Finally

Finding the minimum in a BST is a very intuitive process — just keep going left until you can't go any further. This works because of the BST property that all smaller elements lie to the left. Make sure your code handles edge cases like empty trees and single-node trees, especially in interviews or production-level code. A safe, defensive approach leads to more reliable software.

Algorithm Steps

  1. Start at the root node of the BST.
  2. If the tree is empty, return an error or null.
  3. Iteratively traverse to the left child while it exists.
  4. When a node with no left child is reached, that node contains the minimum value.
  5. Return the value of this node.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

TreeNode* createNode(int val) {
    TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
    node->val = val;
    node->left = node->right = NULL;
    return node;
}

int findMin(TreeNode* root) {
    if (root == NULL) {
        printf("Tree is empty\n");
        exit(1);
    }
    while (root->left != NULL) {
        root = root->left;
    }
    return root->val;
}

int main() {
    TreeNode* root = createNode(5);
    root->left = createNode(3);
    root->right = createNode(7);
    root->left->left = createNode(2);
    root->left->right = createNode(4);
    root->right->left = createNode(6);
    root->right->right = createNode(8);
    printf("%d\n", findMin(root));
    return 0;
}

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...