- Path Sum
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
A leaf is a node with no children.
bool findPath(struct TreeNode* root, int cur, int sum){
if(root == NULL)return false;
cur += root->val;
if(root->left == NULL&&root->right == NULL){
if(cur == sum)return true;
}
return findPath(root->left,cur,sum)||findPath(root->right,cur,sum);
}
bool hasPathSum(struct TreeNode* root, int targetSum){
int cur = 0;
return findPath(root,cur,targetSum);
}