/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
int SUM=0,a=0;
void tree(struct TreeNode* root,int sum){
SUM+=root->val;
if(root->left==NULL&&root->right==NULL){
if(SUM==sum){
a=1;
}
}else{
if(root->left!=NULL){
tree(root->left,sum);
}
if(root->right!=NULL){
tree(root->right,sum);
}
}
SUM-=root->val;
}
bool hasPathSum(struct TreeNode* root, int sum ) {
// write code here
if(root==NULL)
return false;
tree(root,sum);
if(a==0){
return false;
}
else{
return true;
}
}