Minimum Depth or Height of a Binary Tree

Last Updated : 3 May, 2026

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 

Examples

Input: root = [1, 3, 2, 4]

12345

Output: 2
Explanation: Minimum depth is between nodes 1 and 2 since minimum depth is defined as the number of nodes along the shortest path from the root node down to the nearest leaf node.

Input: root = [10, 20, 30, N, 40, N, 60, N, N, 2]

123456

Output: 3

Try It Yourself
redirect icon

Using Recursion - O(n) Time O(h) Space

The idea is to recursively compute the minimum depth by exploring both left and right subtrees.

  • If the node is a leaf, return 1.
  • Otherwise, return 1 + min(left, right). This approach checks all paths before deciding the minimum.

Consider the following tree for example to understand the flow.

heightTree3

minDepth('12') = max(minDepth('8'), minDepth('18')) + 1 = 1 + 1 = 2
because recursively 
minDepth('8') =  min (minDepth('5'), minDepth('11')) + 1 = 1 + 1
minDepth('18') = min (minDepth(NULL), minDepth('NULL)) + 1 = 0 + 1 = 1
minDepth("5") = min (minDepth(NULL), minDepth('NULL)) + 1 = 0 + 1 = 1
minDepth("11") = min (minDepth(NULL), minDepth('NULL)) + 1 = 0 + 1 = 1

C++
#include <iostream>
using namespace std;

// Node structure
class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int val)
    {
        data = val;
        left = nullptr;
        right = nullptr;
    }
};

// Function to find minimum depth using DFS
int minDepth(Node *root)
{

    // Base case: if tree is empty
    if (root == nullptr)
        return 0;

    // If leaf node, depth is 1
    if (root->left == nullptr && root->right == nullptr)
        return 1;

    // Initialize depths as large value
    int left = INT_MAX, right = INT_MAX;

    // If left child exists, recur for left subtree
    if (root->left != nullptr)
        left = minDepth(root->left);

    // If right child exists, recur for right subtree
    if (root->right != nullptr)
        right = minDepth(root->right);

    // Return minimum of left and right subtree depths + 1 (for current node)
    return 1 + min(left, right);
};

int main()
{
    Node *root = new Node(12);
    root->left = new Node(8);
    root->right = new Node(18);
    root->left->left = new Node(5);
    root->left->right = new Node(11);

    cout << minDepth(root);
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// Node structure
typedef struct Node {
    int data;
    struct Node *left;
    struct Node *right;
} Node;

// Function to create a new node
Node* createNode(int data) {
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

// Function to find minimum depth using DFS
int minDepth(Node* root) {
    // Base case: if tree is empty
    if (root == NULL)
        return 0;
    // If leaf node, depth is 1
    if (root->left == NULL && root->right == NULL)
        return 1;
    // Initialize depths as large value
    int left = INT_MAX, right = INT_MAX;
    // If left child exists, recur for left subtree
    if (root->left!= NULL)
        left = minDepth(root->left);
    // If right child exists, recur for right subtree
    if (root->right!= NULL)
        right = minDepth(root->right);
    // Return minimum of left and right subtree depths + 1 (for current node)
    return 1 + (left < right? left : right);
}

int main() {
    Node *root = createNode(12);
    root->left = createNode(8);
    root->right = createNode(18);
    root->left->left = createNode(5);
    root->left->right = createNode(11);
    printf("%d\n", minDepth(root));
    return 0;
}
Java
class Node {
    int data;
    Node left, right;

    public Node(int item) {
        data = item;
        left = right = null;
    }
}

public class BinaryTree {
    Node root;

    // Function to find minimum depth using DFS
    int minDepth(Node root) {
        if (root == null)
            return 0;
        if (root.left == null && root.right == null)
            return 1;
        int left = Integer.MAX_VALUE, right = Integer.MAX_VALUE;
        if (root.left!= null)
            left = minDepth(root.left);
        if (root.right!= null)
            right = minDepth(root.right);
        return 1 + Math.min(left, right);
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(12);
        tree.root.left = new Node(8);
        tree.root.right = new Node(18);
        tree.root.left.left = new Node(5);
        tree.root.left.right = new Node(11);
        System.out.println(tree.minDepth(tree.root));
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

# Function to find minimum depth using DFS
def minDepth(root):
    if root is None:
        return 0
    if root.left is None and root.right is None:
        return 1
    left = float('inf')
    right = float('inf')
    if root.left is not None:
        left = minDepth(root.left)
    if root.right is not None:
        right = minDepth(root.right)
    return 1 + min(left, right)

# Main execution
root = Node(12)
root.left = Node(8)
root.right = Node(18)
root.left.left = Node(5)
root.left.right = Node(11)
print(minDepth(root))
C#
using System;

// Node structure
public class Node
{
  public int data;
  public Node left;
  public Node right;

  public Node(int val)
  {
      data = val;
      left = null;
      right = null;
  }
}

// Function to find minimum depth using DFS
public class GfG
{
  public static int minDepth(Node root)
  {
      // Base case: if tree is empty
      if (root == null)
          return 0;

      // If leaf node, depth is 1
      if (root.left == null && root.right == null)
          return 1;

      // Initialize depths as large value
      int left = Int32.MaxValue, right = Int32.MaxValue;

      // If left child exists, recur for left subtree
      if (root.left!= null)
          left = minDepth(root.left);

      // If right child exists, recur for right subtree
      if (root.right!= null)
          right = minDepth(root.right);

      // Return minimum of left and right subtree depths + 1 (for current node)
      return 1 + Math.Min(left, right);
  }

  public static void Main()
  {
      Node root = new Node(12);
      root.left = new Node(8);
      root.right = new Node(18);
      root.left.left = new Node(5);
      root.left.right = new Node(11);

      Console.WriteLine(minDepth(root));
  }
}
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Function to find minimum depth using DFS
function minDepth(root) {
    if (root === null)
        return 0;
    if (root.left === null && root.right === null)
        return 1;
    let left = Number.MAX_SAFE_INTEGER, right = Number.MAX_SAFE_INTEGER;
    if (root.left!== null)
        left = minDepth(root.left);
    if (root.right!== null)
        right = minDepth(root.right);
    return 1 + Math.min(left, right);
}

// Main execution
let root = new Node(12);
root.left = new Node(8);
root.right = new Node(18);
root.left.left = new Node(5);
root.left.right = new Node(11);
console.log(minDepth(root));

Output
2

Level Order Traversal - O(n) Time O(n) Space

The idea is to traverse the binary tree level by level using a queue. We start from the root node and process all nodes at the current level before moving to the next. A variable depth keeps track of the current level. As soon as we encounter the first leaf node, we return the current depth, because BFS guarantees that this is the minimum depth of the tree.

  • We begin with root and add it to the queue and initialize depth as 1.
  • At every level, we remove current level nodes (using queue size) and push nodes of next level.
  • We increment depth after every level
  • Whenever we encounter a leaf node, we return depth.
C++
#include <iostream>
#include <queue>
using namespace std;

// Node structure
class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int val)
    {
        data = val;
        left = nullptr;
        right = nullptr;
    }
};

// Function to find minimum depth using BFS
int minDepth(Node* root)
{

    // Base case: if tree is empty
    if (!root)
        return 0;

    // Queue to store node along with its depth
    queue<Node*> q;

    // Push root node with initial depth = 1
    q.push(root);
    int depth = 1;

    while (!q.empty())
    {

        int sz = q.size();
        for (int i = 0; i < sz; i++)
        {

            auto node = q.front();
            q.pop();
            
            // If current node is a leaf node, return its depth
            // (first leaf encountered in BFS gives minimum depth)
            if (!node->left && !node->right)
                return depth;

            // If left child exists, push it with depth + 1
            if (node->left)
                q.push(node->left);

            // If right child exists, push it with depth + 1
            if (node->right)
                q.push(node->right);
        }
        depth++;
    }
    return depth;
}

int main()
{
    Node *root = new Node(12);
    root->left = new Node(8);
    root->right = new Node(18);
    root->left->left = new Node(5);
    root->left->right = new Node(11);

    cout << minDepth(root);
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>

// Node structure
typedef struct Node {
    int data;
    struct Node *left;
    struct Node *right;
} Node;

Node* createNode(int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = val;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

typedef struct QueueNode {
    Node* node;
    struct QueueNode* next;
} QueueNode;

typedef struct Queue {
    QueueNode* front;
    QueueNode* rear;
} Queue;

Queue* createQueue() {
    Queue* q = (Queue*)malloc(sizeof(Queue));
    q->front = q->rear = NULL;
    return q;
}

void enqueue(Queue* q, Node* node) {
    QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
    newNode->node = node;
    newNode->next = NULL;
    if (q->rear == NULL) {
        q->front = q->rear = newNode;
        return;
    }
    q->rear->next = newNode;
    q->rear = newNode;
}

Node* dequeue(Queue* q) {
    if (q->front == NULL) return NULL;
    QueueNode* temp = q->front;
    Node* node = temp->node;
    q->front = q->front->next;
    if (q->front == NULL) q->rear = NULL;
    free(temp);
    return node;
}

// Function to find minimum depth using BFS
int minDepth(Node* root) {
    
    // Base case: if tree is empty
    if (!root) return 0;
    
    // Queue to store node along with its depth
    Queue* q = createQueue();
    
    // Push root node with initial depth = 1
    enqueue(q, root);
    int depth = 1;
    while (q->front!= NULL) {
        int sz = 0;
        QueueNode* temp = q->front;
        while (temp!= NULL) {
            sz++;
            temp = temp->next;
        }
        for (int i = 0; i < sz; i++) {
            Node* node = dequeue(q);
            
            // If current node is a leaf node, return its depth
            // (first leaf encountered in BFS gives minimum depth)
            if (!node->left && !node->right) return depth;
            
            // If left child exists, push it with depth + 1
            if (node->left) enqueue(q, node->left);
            
            // If right child exists, push it with depth + 1
            if (node->right) enqueue(q, node->right);
        }
        depth++;
    }
    return depth;
}

int main() {
    Node *root = createNode(12);
    root->left = createNode(8);
    root->right = createNode(18);
    root->left->left = createNode(5);
    root->left->right = createNode(11);
    printf("%d\n", minDepth(root));
    return 0;
}
Java
import java.util.LinkedList;
import java.util.Queue;

// Node structure
class Node {
    int data;
    Node left;
    Node right;

    Node(int val) {
        data = val;
        left = null;
        right = null;
    }
}

public class GfG {
    
    // Function to find minimum depth using BFS
    static int minDepth(Node root) {
        
        // Base case: if tree is empty
        if (root == null)
            return 0;

        // Queue to store node along with its depth
        Queue<Node> q = new LinkedList<>();

        // Push root node with initial depth = 1
        q.add(root);
        int depth = 1;

        while (!q.isEmpty()) {
            int sz = q.size();
            for (int i = 0; i < sz; i++) {
                Node node = q.poll();
                
                // If current node is a leaf node, return its depth
                // (first leaf encountered in BFS gives minimum depth)
                if (node.left == null && node.right == null)
                    return depth;

                // If left child exists, push it with depth + 1
                if (node.left!= null)
                    q.add(node.left);

                // If right child exists, push it with depth + 1
                if (node.right!= null)
                    q.add(node.right);
            }
            depth++;
        }
        return depth;
    }

    public static void main(String[] args) {
        Node root = new Node(12);
        root.left = new Node(8);
        root.right = new Node(18);
        root.left.left = new Node(5);
        root.left.right = new Node(11);

        System.out.println(minDepth(root));
    }
}
Python
from collections import deque

# Node structure
class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None

# Function to find minimum depth using BFS
def minDepth(root):
    
    # Base case: if tree is empty
    if not root:
        return 0
        
    # Queue to store node along with its depth
    q = deque([(root, 1)])
    while q:
        node, depth = q.popleft()
        
        # If current node is a leaf node, return its depth
        # (first leaf encountered in BFS gives minimum depth)
        if not node.left and not node.right:
            return depth
            
        # If left child exists, push it with depth + 1
        if node.left:
            q.append((node.left, depth + 1))
            
        # If right child exists, push it with depth + 1
        if node.right:
            q.append((node.right, depth + 1))
    return depth

if __name__ == '__main__':
    root = Node(12)
    root.left = Node(8)
    root.right = Node(18)
    root.left.left = Node(5)
    root.left.right = Node(11)
    print(minDepth(root))
C#
using System;
using System.Collections.Generic;

// Node structure
public class Node
{
    public int data;
    public Node left;
    public Node right;

    public Node(int val)
    {
        data = val;
        left = null;
        right = null;
    }
}

public class GfG
{
    public static int minDepth(Node root)
    {
        // Base case: if tree is empty
        if (root == null) return 0;
        
        // Queue to store node along with its depth
        Queue<Node> q = new Queue<Node>();
        
        // Push root node with initial depth = 1
        q.Enqueue(root);
        
        int depth = 1;
        while (q.Count > 0)
        {
            int sz = q.Count;
            for (int i = 0; i < sz; i++)
            {
                Node node = q.Dequeue();
                
                // If current node is a leaf node, return its depth
                // (first leaf encountered in BFS gives minimum depth)
              
                if (node.left == null && node.right == null) return depth;
                // If left child exists, push it with depth + 1
              
                if (node.left!= null) q.Enqueue(node.left);
              
                // If right child exists, push it with depth + 1
                if (node.right!= null) q.Enqueue(node.right);
            }
            depth++;
        }
        return depth;
    }

    public static void Main()
    {
        Node root = new Node(12);
        root.left = new Node(8);
        root.right = new Node(18);
        root.left.left = new Node(5);
        root.left.right = new Node(11);
        Console.WriteLine(minDepth(root));
    }
}
JavaScript
class Node {
    constructor(val) {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

// Function to find minimum depth using BFS
function minDepth(root) {
    
    // Base case: if tree is empty
    if (!root)
        return 0;

    // Queue to store node along with its depth
    let q = [];

    // Push root node with initial depth = 1
    q.push({node: root, depth: 1});

    while (q.length > 0) {
        let {node, depth} = q.shift();

        // If current node is a leaf node, return its depth
        // (first leaf encountered in BFS gives minimum depth)
        if (!node.left &&!node.right)
            return depth;

        // If left child exists, push it with depth + 1
        if (node.left)
            q.push({node: node.left, depth: depth + 1});

        // If right child exists, push it with depth + 1
        if (node.right)
            q.push({node: node.right, depth: depth + 1});
    }
    return depth;
}

let root = new Node(12);
root.left = new Node(8);
root.right = new Node(18);
root.left.left = new Node(5);
root.left.right = new Node(11);

console.log(minDepth(root));

Output
2
Comment