4
Good afternoon. I am developing recursion methods that work on a binary tree of research, but I am having great difficulties in recursion.
For example, the method below should return the tree height:
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 the right side has a higher height it continues only checking the left side. My problem with recursion is I don’t know how I make it go both ways, right and left. For example, if I am at the root and she 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.
Recursiveness is a crazy thing, I create a method that calls itself inside the body, it is crazy to imagine. kkk
– user28595
That’s right! Thank you very much ;)
– GGirotto