问题遇到的现象和发生背景
https://leetcode-cn.com/problems/path-sum-iii/
问题相关代码,请勿粘贴截图
class Solution {
int count = 0;
public:
int pathSum(TreeNode* root, int targetSum) {
if(root == nullptr)
return 0;
dfs(root,targetSum);
pathSum(root->left,targetSum);
pathSum(root->right,targetSum);
return count;
}
void dfs(TreeNode* root,int sum)
{
if(root == nullptr)
return;
sum-=root->val;
if(sum == 0)
count++;
dfs(root->left,sum);
dfs(root->right,sum);
}
};