What is wrong with this solution


  • -2
    S

    I had test case [1, 2, null, 9, null, 4, 6], it's expected to return 4, but my solution only return 3; I don't know the reason.

    public class Solution {
    

    public int minDepth(TreeNode root) {
    if (root == null) return 0;
    int left = 0;
    int right = 0;
    if (root.left == null && root.right != null){
    right = minDepth(root.right);
    }
    if (root.left != null && root.right == null){
    left = minDepth(root.left);
    }
    if (left != 0 && right != 0) {
    return Math.min(left, right) + 1;
    } else {
    return Math.max(left, right) + 1;
    }
    }
    }


Log in to reply
 

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