/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
以每一个节点为起点,前序遍历,吃一个则sum就会改变 sum = sum - root->val;
*/
int res = 0;
void dfs(TreeNode* root, int sum) {
if (root == nullptr)return;
if (root->val == sum) {
res++;
}
dfs(root->left, sum - root->val);
dfs(root->right, sum - root->val);
return;
}
int FindPath(TreeNode* root, int sum) { //路径一定向下
if (root == nullptr) {
return 0;
}
dfs(root, sum);
FindPath(root->left, sum);
FindPath(root->right, sum);
return res;
}
};