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>

  1. junmin on 2014-4-5

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

  2. Guest on 2014-6-17

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

  3. Zhaoyu Li on 2014-9-8
  4. Windforce on 2014-10-12

    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.

  5. CopperCash on 2015-1-7

    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)

  6. Vishal on 2015-3-15

    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));
    }

  7. Karthik on 2015-3-15

    amazing solution ji

  8. Yunxiao Zou on 2015-7-30

    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);

    }

    }

  9. Ankit Shah on 2016-1-9

    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));
    }

  10. gao can on 2016-1-11

    Seems wrong

  11. ryanlr on 2016-4-13

    Changed.

  12. juno on 2016-6-8

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

  13. Israel Lopez on 2016-6-28

    It works with only one queue

    private int minimunDepth(TreeNode root){

    int totalDepth =0;

    if(root == null)
    return totalDepth;

    Queue queue = new LinkedList();
    queue.add(root);
    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) )
    queue.add(rootNew);
    else break;
    if (rootNewRight != null && (rootNewRight.rightChild != null || rootNewRight.leftChild != null) )
    queue.add(rootNewRight);
    else break;

    }

    return totalDepth;

    }

  14. rupalph on 2016-7-31

    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)

  15. Ankit Shah on 2016-12-22

    shouldn’t it be || instead of &&

    f(curr.left == null || curr.right == null){ return count;}

  16. Iram22 on 2017-2-14


    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);
    }

Leave a comment

*