I did something similar to MaxDepth but it fails

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

    Failed Testcase :

    Input: [1,2]

    Output: 1

    Expected: 2

  • 0

    Its because the right child node of Node1 returns "null." So when you do

    if (root == null) 
            return 0

    It will return 0+1. Then when you take the min of [2,1] you got your Output. Hope this helps.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.