304 North Cardinal St.
Dorchester Center, MA 02124

# Minimum Depth of Binary Tree LeetCode Solution

## Problem – Minimum Depth of Binary Tree LeetCode Solution

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
``````

Example 2:

``````Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
``````

Constraints:

• The number of nodes in the tree is in the range `[0, 105]`.
• `-1000 <= Node.val <= 1000`

## Minimum Depth of Binary Tree LeetCode Solution in Java

``````public class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;
int left = minDepth(root.left);
int right = minDepth(root.right);
return (left == 0 || right == 0) ? left + right + 1: Math.min(left,right) + 1;

}
}
``````

## Minimum Depth of Binary Tree LeetCode Solution in C++

``````int minDepth(TreeNode* root) {
if (root == NULL) return 0;
queue<TreeNode*> Q;
Q.push(root);
int i = 0;
while (!Q.empty()) {
i++;
int k = Q.size();
for (int j=0; j<k; j++) {
TreeNode* rt = Q.front();
if (rt->left) Q.push(rt->left);
if (rt->right) Q.push(rt->right);
Q.pop();
if (rt->left==NULL && rt->right==NULL) return i;
}
}
return -1; //For the compiler thing. The code never runs here.
}
``````

## Minimum Depth of Binary Tree LeetCode Solution in Python

``````def minDepth(self, root):
if not root:
return 0
queue = collections.deque([(root, 1)])
while queue:
node, level = queue.popleft()
if node:
if not node.left and not node.right:
return level
else:
queue.append((node.left, level+1))
queue.append((node.right, level+1))
``````
##### Minimum Depth of Binary Tree LeetCode Solution Review:

In our experience, we suggest you solve this Minimum Depth of Binary Tree LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Depth of Binary Tree LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Minimum Depth of Binary Tree LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions