判断是否存在从根结点出发的一条路径,使其路径上所有结点之和等于给定值
bool hasPathSum(TreeNode *root, int sum)
{
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if(root == nullptr) return false;
if(root->left == nullptr && root->right == nullptr)
return sum == root->val;
return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val);
}
};
}