Good evening, I'm studying binary trees and I came across an exercise that asks us to count the knots. I imitated adapting an algorithm that calculates height and worked out ...
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;
}
}
But I do not understand very well, I already have doubts about recursion and I believe this is the point of my doubt. In this question I declare two variables to receive what comes from the left and the right and in the return I added the two and added 1. From this follows, if I take the "+ 1" of the return it zeroes the amount of nodes, I understood that every time it passes for one of the nodes it adds 1, blz, but, what gets these two variables count and contd, before that I tried to return return conta_nos(r->esq) + conta_nos(r->dir) + 1;
but it did not work very well, as I wrote I carry doubts about recursion and I feel like I'm getting in the way of trees.