For a given binary tree and a sum, I have written the following function to check whether there is a root to leaf path in that tree with the given sum.
/*
//A binary tree node
struct Node
{
int data;
struct Node* left, * right;
};
*/
bool hasPathSum(Node *node, int sum)
{
if(!node)
return sum==0;
return ( hasPathSum(node->left, sum-node->data) ||
hasPathSum(node->right, sum-node->data) );
}
Are there any edge cases in which the code will break? Also, do comment on the code style.