# Help please URGENT! creating linked list from tree leaves

Discussion in 'C' started by ayman88, Feb 28, 2010.

1. ### ayman88New Member

Joined:
Feb 28, 2010
Messages:
1
0
Trophy Points:
0
hii , I'll appreciate it if someone please help me with this problem
I want to create a linked-list from the leaves of a tree ordered from right to left .
without using a Static or global variable

typedef struct t_node {
int data;
struct t_node *left;
struct t_node *right;
} T_NODE;

typedef struct list_node
{
int data;
struct list_node *next;
} L_NODE;

the function header : L_NODE* leavesRightToLeft (T_NODE* root, 1 more variable )
for example :
Code:
```                                  3
/    \
2      5
\    /  \
4   7   9
/
1

```
and the function will return a pointer to linked list that will be as following :
1 -> 7 -> 4

I've tried to solve it but i have a problem with the pointers, the idea works, but can you help me with the pointers please
or you can just give me another solution ?
Code:
```Lnode *leavesRightToLeft(Tree *root,Lnode *last)
{
Lnode *p,*p2,*lastleft,*lastright;
if ( ( root->left==NULL) && (root->right==NULL) )
{
p= (Lnode*) malloc (sizeof(Lnode));
p->next=NULL;
p->data=root->value;
last=p;
return p;
}
else if ( !root->left )
{
return leavesRightToLeft(root->right,last);
}
else if ( !root->right )
{
return leavesRightToLeft(root->left,last);
}
else
{
p=leavesRightToLeft(root->right,lastright);
p2=leavesRightToLeft(root->left,lastleft);
lastright->next=p2;
last=lastleft;
return p;
}
}

```