解题思路:
(1)依次判断左右子树是否满足即可,递归求解
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root==NULL) return false;
if(root->left==NULL&&root->right==NULL&&root->val==sum) return true;
else return hasPathSum(root->left, sum-root->val)||hasPathSum(root->right, sum-root->val);
}
};