/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param sum int整型
* @return int整型
*/
int check(TreeNode* root,int sum,int expect)
{
if(!root)
{
return 0;
}
if(expect==sum+root->val)
{
return 1+check(root->left,0,0)+check(root->right,0,0);
}
else
{
return check(root->left,sum+root->val,expect)+check(root->right,sum+root->val,expect);
}
}
int count=0;
void dfs(TreeNode* root,int sum)
{
if(!root)
{
return;
}
count+=check(root,0,sum);
dfs(root->left,sum);
dfs(root->right,sum);
}
int FindPath(TreeNode* root, int sum) {
dfs(root,sum);
return count;
}
};