//思路具体看题解。我是参考题解思路写的。
class Solution {
public:
void solve(TreeNode* root,int sum,int &count)
{
if(root==nullptr)
return;
if(root->val==sum)
{
count++;
}
solve(root->left, sum-root->val, count);
solve(root->right, sum-root->val, count);
}
int FrontOrder(TreeNode* root,int sum)
{
if(root==nullptr)
return 0;
int current=0;
solve(root, sum, current);//处理当前节点
int left=0;
int right=0;
if(root->left)
{
left=FrontOrder(root->left, sum);//递归两个孩子
}
if(root->right)
{
right=FrontOrder(root->right, sum);
}
return left+right+current;
}
int FindPath(TreeNode* root, int sum) {
return FrontOrder(root, sum);
}
};