You have a compilation error because you do not always return something:
if(root.getLeft()!=null){
search(root.getLeft(), node);
}
if(root.getRight()!=null){
search(root.getRight(), node);
}
This will fix the compilation error, but not the algorithm:
if(root.getLeft()!=null){
return search(root.getLeft(), node);
}
if(root.getRight()!=null){
return search(root.getRight(), node);
}
This should fix the algorithm:
if(root.getLeft()!=null && search(root.getLeft(), node)) {
return true;
}
if(root.getRight()!=null && search(root.getRight(), node)){
return true;
}
return false;
source
share