Whats the bug in this Java solution


#1

public class Solution {
int res;
public int kthsmallest(TreeNode A, int B) {

    kthsmallest2(A , B);
    return res;
}

public void kthsmallest2(TreeNode root , int k){
    
    if(root == null){
        return;
    }
    
    //System.out.println(root.val + " " + k);
    kthsmallest2(root.left , k);
   

    if(k == 1){
        res = root.val;
        return;
    }
   
    k--;
     
    kthsmallest2(root.right , k);
    
    
}

}.


#2

k should be 0. Its a small mistake. Think about it.


#3

You must declare k globally.


#4

(k == 1) - this seems to be the issue, you are finding (k-1)th smallest element by doing this