This came to me while traversing a tree i wrote this:
if( node==NULL || node->left==NULL && node->left==NULL) return 1;
and the given solution had:
if(!node || !node->left && !node->right) return 1;
is there a difference between the two i know it might be something stupid but it just click right now.
On this question https://practice.geeksforgeeks.org/problems/children-sum-parent/1/?track=PC-W6-T&batchId=154
this code gives error
int isSumProperty(Node *node){
int l=0,r=0;
if( node==NULL || node->left==NULL && node->left==NULL) return 1;
if(node->left) l=node->left->data; else l=0;
if(node->right) r=node->right->data; else r=0;
return ( node->data == l+r && isSumProperty(node->left) && isSumProperty(node->right) );
}
but replace the first if statement with the other statement works can you guys explain.
the error comes with the test case: 2 N 1 N 2 N 1 N 1 N 2 N 1