1ms Java recursive solution


  • 0
    F
      /**
     * Accepted
     * 1ms
     * @param root
     * @return
     */
    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 || right == 0) return left > right ? left +1 : right +1;
        return left > right ? right + 1 : left + 1;
    }

Log in to reply
 

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