/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
bool dfs(TreeNode* root, int sum) {
if (!root) {
return false;
}
sum = sum - root->val;
if (!root->left && !root->right && sum == 0) {
return true;
}
return dfs(root->left, sum) || dfs(root->right, sum);
}
bool hasPathSum(TreeNode* root, int sum) {
if(!root) {
return false;
}
return dfs(root, sum);
}
};