Neglect non leaf nodes by assigning them INT_MAX value


  • 0
    S

    Easy to Understand Java Solution

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


Log in to reply
 

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