Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Read full article from Yu's Coding Garden : leetcode Question 66: Path Sum I
bool
hasPathSum(TreeNode *root,
int
sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if
(root==NULL) {
return
false
;}
if
( (root->left==NULL) && (root->right==NULL) && (sum-root->val==0) ) {
return
true
;}
return
(hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val));
}