原题地址(简单)
方法1—回溯
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(!root) return false;
sum -= root->val;
if(!root->left && !root->right){
if(sum == 0) return true;
}
return hasPathSum(root->left, sum) || hasPathSum(root->right, sum);
}
};
**