/**
* 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) {
return hasPathSum(root,sum,0);
}
private:
bool hasPathSum(TreeNode *root, int sum, int CurrSum){
if(!root)return false;
CurrSum+=root->val;
if((!root->left)&&(!root->right))return CurrSum==sum;
return hasPathSum(root->left,sum,CurrSum)||hasPathSum(root->right,sum,CurrSum);
}
};
【LeetCode】Path Sum
最新推荐文章于 2020-07-07 13:07:47 发布