Can you tell me ,what is wrong with my approach for the question to replace each node by sum of child nodes(leaving leaf nodes unchanged)? I don't understand why I am getting a segmentation fault?

node* sum(node* root)
{
if(root==NULL)
{
return root;
}
node *l=sum(root->left);
node *r=sum(root->right);
int temp=root->data;
root->data=(l->data)+(r->data)+temp;
return root;
}

Hey @isingh

Here ,use l->data only if l is not null
Similarly add r->data if r is not NULL