112. Path Sum

targetSumの値からtreeの値をrootから引いていく。0になったときにその下にノードがなければtrue。

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr){
            return false;
        }
        
        targetSum = targetSum-root->val;
        if(targetSum == 0 && root->left == NULL && root->right == NULL ){
            return true;
        }
        bool leftResult = hasPathSum(root->left, targetSum);
        bool rightResult = hasPathSum(root->right, targetSum);
        
        return leftResult || rightResult;
    }
};