题目:
代码(首刷看解析,暴力法):
class Solution {
public:
long rootSum(TreeNode* root,long targetSum){
if(!root) return 0;
long res=0;
if(root->val==targetSum){
res++;
}
res+=rootSum(root->left,targetSum-root->val);
res+=rootSum(root->right,targetSum-root->val);
return res;
}
long pathSum(TreeNode* root, long targetSum) {
if(!root) return 0;
long res =rootSum(root,targetSum);
res+=pathSum(root->left,targetSum);
res+=pathSum(root->right,targetSum);
return res;
}
};
int类型大小不够,需要long