public int minDepth(TreeNode root) {//solved after consulting the answer
        if (root == null) return 0;
        int ldepth = minDepth(root.left);
        int rdepth = minDepth(root.right);
        if (ldepth == 0 && rdepth == 0) {
            return 1;
        }
        if (ldepth == 0) {
            ldepth = Integer.MAX_VALUE;
        }
        if (rdepth == 0) {
            rdepth = Integer.MAX_VALUE;
        }
        return Math.min(ldepth, rdepth) + 1;
    }