VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-minimum-depth-of-a-binary-tree/

⇱ Minimum Depth or Height of a Binary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

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


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.

Output
2
Comment
Article Tags: