correct me if I am wrong
#include<stdio.h>
struct node
{
int data;
struct node *left;
struct node * right;
}*root;
int sum(int s,struct node *p,int ar[],int l)
{
if(p == NULL )
{
return 0;
}
if(p->left == NULL && p->right == NULL)
{
if( s - p->data == 0)
{
ar[l++] = p->data;
int i;
for( i = 0 ;i < l ;i++)
{
printf("%d ",ar[i]);
}
printf("\n");
}
}
ar[l++] = p->data;
sum(s - p->data, p->left , ar , l);
sum(s - p->data, p->right , ar, l);
return 0;
}
struct node * getnode(int k)
{
struct node *temp = malloc(sizeof(struct node));
temp->data = k;
temp->left= NULL;
temp->right = NULL;
return temp;
}
main()
{
int ar[50],value;
root = getnode(5);
root->left= getnode(2);
root->right = getnode(2);
root->left->left = getnode(7);
root->left->right = getnode(8);
root->right->left = getnode(3);
root->right->right = getnode(7);
value = 14;
sum(value,root,ar,0);
return 0;
}
On Fri, Nov 11, 2011 at 12:38 PM, aniket chatterjee <[email protected]>wrote:
> Write a recursive function that will store each root to leaf path in an
> array. Now for each root to leaf path find the subarray which sums up to X.
>
> On Thu, Nov 10, 2011 at 11:53 PM, AMAN AGARWAL <[email protected]>wrote:
>
>> Hi All,
>>
>> Please give me the solution of this problem.
>>
>> A binary tree and a number X is given. Find all the paths(not necessarily
>> starting from root) such that the sum equals X.
>>
>>
>> Regards,
>> Aman.
>>
>> --
>> AMAN AGARWAL
>> "Success is not final, Failure is not fatal: It is the courage to
>> continue that counts!"
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
*UTKARSH SRIVASTAV
CSE-3
B-Tech 3rd Year
@MNNIT ALLAHABAD*
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.