# LeetCode – Minimum Depth of Binary Tree (Java)

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.

Thoughts

LinkedList is a queue in Java. The add() and remove() methods are used to manipulate the queue.

Java Solution

```/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int minDepth(TreeNode root) { if(root == null){ return 0; }   LinkedList<TreeNode> nodes = new LinkedList<TreeNode>(); LinkedList<Integer> counts = new LinkedList<Integer>();   nodes.add(root); counts.add(1);   while(!nodes.isEmpty()){ TreeNode curr = nodes.remove(); int count = counts.remove();   if(curr.left == null && curr.right == null){ return count; }   if(curr.left != null){ nodes.add(curr.left); counts.add(count+1); }   if(curr.right != null){ nodes.add(curr.right); counts.add(count+1); } }   return 0; } }```
Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• Kishore Srinivas

Actually,this problem is not the exact reverse of the max depth where we select the maximum depth in each subtree + 1, your code will fail for the input [1,2] where 1 is root and 2 is the left child, it will return 1, but the answer is 2.

• Haohua Wang

possible solution:

public static int minDepth(TreeNode root) { r
if (root == null) { return 0; }
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}

• Manu Sinha

``` public int minDepth(TreeNode root) {```

``` if(root == null){ return 0; } return helper(root, 1); } private int helper(TreeNode node, int level){ if(node.left == null && node.right == null){ return level; } int lDepth = Integer.MAX_VALUE; if(node.left != null){ lDepth = helper(node.left , level + 1); } int rDepth = Integer.MAX_VALUE; if(node.right != null){ rDepth = helper(node.right , level + 1); } ```

``` return Math.min(lDepth , rDepth); } ```

• Iram22

``` static int min=Integer.MAX_VALUE; private static void minDepth(Node n,int x){ if(n==null) return; if(n.left==null && n.right==null){ min=Math.min(min,x+1); } minDepth(n.left,x+1); minDepth(n.right,x+1); } ```

• Ankit Shah

shouldn’t it be || instead of &&
``` f(curr.left == null || curr.right == null){ return count;} ```

• rupalph

Correct recursive solution:
``` public int minDepth(TreeNode node,int depth) {```

``` if (node == null) return 0; int depth1=minDepth(node.left,depth+1); int depth2=minDepth(node.right,depth+1); return (Math.min(depth1,depth2 )+depth); } ```

```Call above method as minDepth(root,0) ```

• Israel Lopez

It works with only one queue

private int minimunDepth(TreeNode root){

int totalDepth =0;

if(root == null)

totalDepth++;
TreeNode rootNew, rootNewRight;

while (!queue.isEmpty()){

TreeNode node = queue.poll();
root = node;
totalDepth++;
rootNew = node.leftChild;
rootNewRight = node.rightChild;

if (rootNew != null && (rootNew.leftChild !=null || rootNew.rightChild!=null) )
else break;
if (rootNewRight != null && (rootNewRight.rightChild != null || rootNewRight.leftChild != null) )
else break;

}

}

• juno

return Math.max(minDepth(root.left), minDepth(root.right)) +1;

• ryanlr

Changed.

• gao can

Seems wrong

• Ankit Shah

you can further optimzer your solution Vishal by doing this: not need for the other code in between.

private int minDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}

• Yunxiao Zou

A simpler Java Solution:

public class Solution {

int minHeight = -1;

public int minDepth(TreeNode root) {

if (root == null)

return 0;

iterateTree(root, 1);

return minHeight;

}

private void iterateTree(TreeNode root, int i) {

if (root.left == null && root.right == null) {

if (minHeight == -1)

minHeight = i;

else if (minHeight > i)

minHeight = i;

}

if (root.left != null)

iterateTree(root.left, i + 1);

if (root.right != null)

iterateTree(root.right, i + 1);

}

}

• Karthik

amazing solution ji

• Vishal

public int minDepth(TreeNode root) {
if(root == null) return 0;
if(root.left ==null) return 1+minDepth(root.right);
if(root.right ==null) return 1+minDepth(root.left);
return 1+Math.min(minDepth(root.left),minDepth(root.right));
}

• CopperCash

python version recursive min depth:

def min_depth(self, depth=1):
left, right = self.left, self.right
left_min = left.min_depth(depth + 1) if left else depth
right_min = right.min_depth(depth + 1) if right else depth
return min(left_min, right_min)

• Windforce

I don’t understand the
return 0;
line. It seems the while loop should return the right answer. But as I tried, I can’t pass oj without this line. However, I changed it to ” return 1″ or “return 1000”. They all get passed. Weird.

• Guest

Is this solution for Depth of tree or height of tree???

• junmin

move thisif(curr.left == null && curr.right == null){ return count;} to the front of the other two ifs will optimize the solution