class Solution {
unordered_map<long long, long long> mp;
int res;
public:
void dfs(TreeNode* root, long long num, int targetSum) {
if (root == nullptr) {
return ;
}
long long temp = num + root->val;
if (mp[temp - targetSum] >= 1) {
res += mp[temp - targetSum];
}
mp[temp]++;
dfs(root->left, temp, targetSum);
dfs(root->right, temp, targetSum);
mp[temp]--;
}
int pathSum(TreeNode* root, int targetSum) {
mp[0] = 1;
dfs(root, 0, targetSum);
return res;
}
};
08-30
1231
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-12
722
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)