Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help please URGENT! creating linked list from tree leaves (http://www.go4expert.com/forums/help-urgent-creating-linked-list-tree-t21151/)

ayman88 28Feb2010 17:46

Help please URGENT! creating linked list from tree leaves
 
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;
        }
}

Thanks in advanced.


All times are GMT +5.5. The time now is 12:52.