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

20 thoughts on “LeetCode – Minimum Depth of Binary Tree (Java)”

  1. 2 different solutions:

    int minDepthIterative(Node head)
    {
    if (n == null) return 0;

    int depth = 1;
    Queue currLevel = new LinkedList();
    currLevel.add(head);
    Queue nextLevel = new LinkedList();

    while(true)
    {
    while(!currLevel.isEmpty())
    {
    Node n = currLevel.remove();

    if (n.left == null || n.right == null) return depth;

    nextLevel.add(n.left);
    nextLevel.add(n.right);
    }

    depth++;
    currLevel = nextLevel;
    nextLevel = new LinkedList();
    }

    }

    /* This solution is problematic because it goes down all of the left tree where the right can be much shorter.*/
    int minDepthRecurse(Node n)
    {
    if (n == null) return 0;

    if (n.left == null || n.right == null) return 1;

    return Math.min(minDepth(n.left), minDepth(n.right)) + 1;
    }

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

  3. possible solution:

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


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


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

  6. 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)

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

    }

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

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

    }

    }

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

  11. 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)

  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.

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

Leave a Comment