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