Given solution is not working


#1

private static int current = 0;
private static int result = 0;

public int kthsmallest(TreeNode A, int B) {
    explore(A,B);
    return result;
}

private void explore(TreeNode A, int B){
    if(A==null) return;
    explore(A.left,B);
    current++;
    if(current==B){
        result = A.val;
        return;
    }
    explore(A.right,B);
}

this is not working properly even this is given as a complete solution. please help.
7 2 1 3 -1 -1 -1 -1
2

Your function returned the following :

1

The expected returned value :

2


#2

I faced the same issue.
This could be because you have used “current” as global variable. Resetting it in kthsmallest() should resolve the issue.