class Solution {
public:
/**
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
int sign=0; //sign=1时表示找到路径
void qiuhe(TreeNode* root,int s,int sum){ //节点值自增 传入子节点
s+=root->val;
if(root->left!=NULL) qiuhe(root->left,s,sum);
if(root->right!=NULL) qiuhe(root->right,s,sum);
if(root->left==NULL&&root->right==NULL) if(s==sum) sign=1; //当是叶子节点时,判断是否是要的值
}
bool hasPathSum(TreeNode* root, int sum) {
// write code here
if(root!=NULL) qiuhe(root,0,sum); //从根节点开始遍历
else return false;
if(sign==0) return false;
else return true;
}
};