题意:求树根到叶子的路径上的权值和
code:
class Solution {
bool ans=false;
int Sum;
void dfs(int s,TreeNode *p){
if(p==NULL){
if (s==Sum)
ans = true;
return;
}
dfs(s+p->val,p->left);
dfs(s+p->val,p->right);
}
public:
bool hasPathSum(TreeNode *root, int sum) {
Sum = sum;
if (root==NULL)
return false;
dfs(root->val,root->left);
dfs(root->val,root->right);
return ans;
}
};