Reputation: 53
My problem is that this method "isLargest" doesn't end when it meets "return true;". Even if the condition in "else if" is true, it seems to just skip it and always returns "false". I even printed a word "test" to prove that. When I use the method, it says "test" and returns false.
I tried to put this "return false" inside an else or another else if, but then is says that "This method must return boolean type" and proposes me to add return statement. How do I deal with that?
public boolean isLargest(Node tmp, Node parent){
if(tmp.value > parent.value){
parent = parent.right;
tmp.isLargest(tmp, parent);
}
else if(parent.value == tmp.value){
System.out.println("test");
return true;
}
return false;
Upvotes: 3
Views: 1513
Reputation: 2090
When you have a recursive call, you need to return the value of the recursive call.
public boolean isLargest(Node tmp, Node parent){
if(tmp.value > parent.value){
parent = parent.right;
return tmp.isLargest(tmp, parent);
}
else if(parent.value == tmp.value){
System.out.println("test");
return true;
}
return false;
Upvotes: 7