<pre name="code" class="cpp">class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
return check(root,sum,0);
}
bool check(TreeNode *root,int sum,int curSum){
if(root == NULL)
return false;
if(root -> left == NULL && root -> right == NULL && sum == curSum + root -> val)
return true;
return check(root -> left,sum,curSum + root -> val) || check(root -> right,sum,curSum + root -> val);
}
};
[LeetCode] Path Sum
最新推荐文章于 2020-07-07 13:07:47 发布