/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool judge(struct TreeNode* root,int now,int sum){
now+=root->val;
if(!root->left&&!root->right){
if(now==sum) return true;
return false;
}
bool a=false,b=false;
if(root->left) a=judge(root->left,now,sum);
if(root->right) b=judge(root->right,now,sum);
return a||b;
}
bool hasPathSum(struct TreeNode* root, int targetSum) {
if(!root) return false;
return judge(root,0,targetSum);
}
结果:
标签:right,return,struct,路径,112,TreeNode,now,root,总和 From: https://www.cnblogs.com/llllmz/p/18055344