/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
bool flag=false;
bool hasPathSum(TreeNode* root, int sum) {
Find(root,sum);
return flag;
// write code here
}
void Find(TreeNode *root,int sum){
//递归出口
if(root==NULL){
return;
}
//dfs路径已走完
else if(!root->left&&!root->right&&root->val==sum){
flag=true;
}
//继续走dfs路径
else{
if(root->left){
Find(root->left,sum-root->val);
}
if(root->right){
Find(root->right,sum-root->val);
}
}
}
};