55561bc4创建于 2022年12月27日历史提交
/*
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.

Space: O(log(n)) (due to recursive calls)
Time: O(n)
*/

bool hasPathSum(struct TreeNode* root, int targetSum){
    if (!root)
        return false;
    if (!root->left && !root->right)
        return root->val == targetSum;
    return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val);
}