2021-06-06路径总和
作者:互联网
判断是否存在从根结点出发的一条路径,使其路径上所有结点之和等于给定值
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);
}
};
}
标签:hasPathSum,val,sum,nullptr,return,2021,06,root,总和 来源: https://blog.csdn.net/weixin_45792025/article/details/117620177