<pre>
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
recursive( root,sum);
return res;
//
//
//没有选择直接跳出多层递归 可能会影响效率
}
bool res=false;
void recursive(TreeNode* root,int target)
{
if(root==NULL)
return;
if(root->left==NULL&&root->right==NULL&&target==(root->val))
{
res=true;
}
recursive(root->left,target-(root->val));
recursive(root->right,target-(root->val));
}
};
</pre>