# Find the Minimum Depth of Binary Tree in C++ – LeetCode Solution [Easy]

Problem
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.

Note: A leaf is a node with no children.

Example 1:

Input: root = [3, 9, 20, null, null, 15, 7]
Output: 2
###### Find the Minimum Depth of Binary Tree Solution

Approach

This problem could be solved by following the idea explained in maximum depth of the binary tree in this post.

Pseudocode

1. Base case: return 0, if root is NULL.2. Get the minimum height of left subtree (recursively).3. Get the minimum height of right subtree (recursively).4. Return the min of (left-subtree height + 1, right-subtree height + 1).

Time Complexity: O(n), where n is the number of nodes in Binary Tree

Code Implementation

class Solution {
public:
int minDepth (TreeNode* root) {
if (root==NULL) {
return 0;
}

int l = minDepth(root->left);
int r = minDepth(root->right);

if (l == 0) {
return 1 + r;
} else if (r == 0) {
return 1 + l;
} else {
return 1 + min(l, r);
}

}
};


Output

Your input[2,null,3,null,4,null,5,null,6]Output5Expected5