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