题目描述:
https://leetcode-cn.com/problems/path-sum-iii/
代码:
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
int res = 0;
if(!root)
return 0;
res+=Psum(root, sum);
res+=pathSum(root->left, sum);
res+=pathSum(root->right, sum);
return res;
}
int Psum(TreeNode *root, int sum)
{
int res = 0;
if(!root)
return res;
sum-=root->val;
if(sum==0)
res++;
res+=Psum(root->left, sum);
res+=Psum(root->right, sum);
return res;
}
};
网友评论