/**
* Definition for binary tree
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!root)
return false;
int left=sum-root->val;
if(left==0&&root->left==NULL&&root->right==NULL)
return true;
return hasPathSum(root->left,left)||hasPathSum(root->right,left);
}
};
Leetcode: Path Sum
最新推荐文章于 2021-05-11 15:07:28 发布