1、
2、
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int cnt=0;
int pathSum(TreeNode* root, int sum) {
help(root,sum);
return cnt;
}
void help(TreeNode* root, int sum)
{
if(root==nullptr) return;
helper(root,sum);
help(root->right,sum);
help(root->left,sum);
}
void helper(TreeNode* root, int sum)
{
if(root==nullptr) return;
if(root->val==sum) cnt++;//递归减法当他相等时满足条件
helper(root->left,sum-root->val);
helper(root->right,sum-root->val);
}
};