Can any one please explain this
For the same question above, the solution given was:
Code:
int hasPathSum(struct tree_node* node, int sum) {
// return true if we run out of tree and sum==0
if (node == NULL) {
return(sum == 0);
}
else {
// otherwise check both subtrees
int subSum = sum - node->element;
return(hasPathSum(node->left, subSum) ||
hasPathSum(node->right, subSum));
}
}
I don't understand how if (node ==NULL) return (sum ==0) checks the logic (if sum == 0). Any help is greatly appreciated.