Good afternoon. I'm developing recursive methods that work on a binary search tree, but I'm having a hard time recursion.
For example, the method below should return the height of the tree:
public int altura(){
return altura(root,0);
}
private int altura(Node n, int cont){
if(n==null) return 0;
cont += 1;
if(n.left != null)
return altura(n.left, cont);
if(n.right != null)
return altura(n.right, cont);
return cont;
}
however, it returns only the height of the nodes to the left side of the root, and when those on the right side have a higher height it only continues to check the left side. My problem with recursion is that I do not know how I can make it go to both sides, right and left. For example, if I am in the root and it has two children, I want the method to be applied both to the left and to the right, but in the method below it only applies to the left.