so I'm supposed to write a program to print the kth smallest element in a binary search tree. This is the code I have. Sad as this is, I've been staring at my code for 45 minutes and I just can't seem to find my mistake. Could someone help me out?
let res;
function kthLargestInBST(t, k) {
helper(t, k, 1);
return res;
}
function helper(t, k, curr) {
if (t === null) return;
helper(t.left, k, curr);
if (curr === k) {
res = t.value;
}
curr++;
helper(t.right, k, curr);
}
curr variable is local function parameter, when you modify it then its new value is NOT available at upper level. In other words, when you go one level up then its values is reset to 1.
must be something like that