/**
* 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:
int path(TreeNode* root, int sum) {
if(!root) return 0;
sum -= root->val;
int temp = 0;
if(sum == 0) temp = 1;
return temp + path(root->left, sum) + path(root->right, sum);
}
int pathSum(TreeNode* root, int sum) {
if(!root) return 0;
return path(root, sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
}
};
/**
* 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:
int ans = 0;
void path(TreeNode* root, int sum) {
if(!root) return ;
sum -= root->val;
if(sum == 0) ans++;
path(root->left, sum);
path(root->right, sum);
}
void preOreder(TreeNode* root, int sum) {
if(!root) return ;
path(root, sum);
preOreder(root->left, sum);
preOreder(root->right, sum);
}
int pathSum(TreeNode* root, int sum) {
preOreder(root, sum);
return ans;
}
};