educative.io

Why am i getting wrong output in result

why am i getting wrong output in evaluation console. However, I get right result on my IDE

class CheckKthMax {
static int counter = 0;

public static int findKthMax(Node root, int k) {
    if (root == null) {
        return -1;
    }

    int result = -1;

    if(root.getRightChild() != null) {
        result = findKthMax(root.getRightChild(), k);
    }

    if(counter != k){
        counter++;

        if(counter == k) {
            return root.getData();
        }
    }else{
        return result;
    }



    if(root.getLeftChild() != null) {
        result = findKthMax(root.getLeftChild(), k);
    }

    return result;

}

// public static void main(String args[]) {

//     binarySearchTree bsT = new binarySearchTree();

//     bsT.add(6);
//     bsT.add(4);
//     bsT.add(9);
//     bsT.add(5);
//     bsT.add(2);
//     bsT.add(8);
//     bsT.add(12);
//     bsT.add(10);
//     bsT.add(14);

//     System.out.println(findKthMax(bsT.getRoot(),3));
// }

}


Course: https://www.educative.io/collection/5642554087309312/5724822843686912
Lesson: https://www.educative.io/collection/page/5642554087309312/5724822843686912/5682624991330304

Hello,

I hope you’re doing fine. Your code is correct and it’s showing correct output.
Please try to run it again and attach screenshot if you face similar issue again.

Happy Learning!

1 Like

Thank you for the response


Course: https://www.educative.io/collection/10370001/5347133077061632
Lesson: https://www.educative.io/collection/page/10370001/5347133077061632/6248391839318016

1 Like