I have to do a function where, given an int n and a binary search tree, i have to convert the level n of the bst int a linked list. for example if given the numer 2 and this tree
2
/ \
5 3
i have to make a liked list with 5 - 3
I have problems getting to the level given, and then getting each node on that level, because if i do reach the level i don't know how to reach the next node. Meaning, i can only get to the level on only one branch, and i cant think of any way to do it recursively.
so this is the struct for the bst and the linked chain:
struct nodo {
info_t dato;
nodo *anterior;
nodo *siguiente;
};
struct rep_cadena {
nodo *inicio;
nodo *final;
};
struct rep_binario {
info_t dato;
rep_binario *izq;
rep_binario *der;
};
and this is the function i cant figure out:
cadena_t nivel_en_binario(nat l, binario_t b)
i have tried using another function i already made, that calculates the height of a tree, but i can't stop on the wanted level.
nat altura_binario(binario_t b) {
if (b==NULL) return 0;
else return maximo(altura_binario(b->izq), altura_binario(b->der))+ 1;
}
where maximo() returns the highest number between the two given numbers.