1
Good evening, I’m studying binary trees and I came across an exercise that asks me to count the knots. I did it by adapting an algorithm that calculates height and works...
int conta_nos (arvore *r) {
if (r == NULL) return 0;
else {
int conte = conta_nos (r->esq);
int contd = conta_nos (r->dir);
return conte + contd + 1;
}
}
However I did not understand it very well, I already carry recursion doubts and I believe that this is the point of my doubt. In this question I declared two variables to receive what comes from left and right and in the return I added the two and 1. Then it follows, if I take the "+1" of the return he Zera the amount of us, I understood that every time he passes by one of the nodes he adds 1, blz, but, what gets these two variables count and contd, before that tried to return return conta_nos(r->esq) + conta_nos(r->dir) + 1;
but it didn’t work out so well, as I wrote I carry recursion doubts and I feel like I’m getting stuck in trees.
Certainty that
return 1 + conta_nos(r->esq) + conta_nos(r->dir);
didn’t work? Because it should– mercador
I may have done something wrong, I’ll check, but I still have doubts about what the two variables receive
– Marcelo de Sousa