I've been asked by my teachers to write a function in C that mirrors a binary tree, and by that I mean that inverts the tree. I've been struggling with this question because the solution doesn't make any sense to me. The data struct we use is the following:
typedef struct nodo {
int valor;
struct nodo *esq, *dir;
} *ABin;
and the solution is:
void mirror (ABin *a) {
ABin c = *a;
if (c == NULL);
else {
ABin e = c -> esq;
ABin d = c -> dir;
c -> esq = d;
c -> dir = e;
mirror (&(c -> esq));
mirror (&(c -> dir));
}
}
My biggest concern here is the use of pointers or not. I don't understand why, when we call the function recursively, we have to use &
when esq
and dir
are already pointers to the struct nodo
type?