I am trying to build a linked list, with the elements at a certain depth
I came up with this:
void nivel(ABin a, int k, SList *l, int level){
if (!a) return;
if(k == level){
SList n = (SList)malloc(sizeof(struct slist));
n->value = a->value;
n->next=(*l);
(*l) = n;
return;
}else{
nivel(a->left, k, l, level+1);
nivel(a->right, k, l, level+1);
}
}
It does work
But the exercise asks to use this header: SList nivel (ABin a, int n)
I have tried with a void to practice. But can't figure out how to make the one that returns the linked lists.
Data from structure and binary tree:
typedef struct slist
{
int value;
struct slist* next;
} *SList;
typedef struct arvbin* ABin;
typedef struct arvbin
{
int value;
ABin right;
ABin left;
} arvb;
EDIT:
<---------------working with header in the exercise-----------> // A thank you to Politank-Z
SList nivel_(ABin a, int k){
SList *l;
nivel(a, k, l, 1);
return l;
}
void nivel(ABin a, int k, SList *l, int level){
if (!a) return;
if(k == level){
SList n = (SList)malloc(sizeof(struct slist));
n->value = a->value;
n->next=(*l);
(*l) = n;
return;
}else{
nivel(a->left, k, l, level+1);
nivel(a->right, k, l, level+1);
}
}