判断二叉树,是否存在一条根到叶子的路径和,与一个指定的目标数字相等

来源:互联网 发布:linux suspend线程 编辑:程序博客网 时间:2024/05/16 18:42
struct TreeNode{    TreeNode* left;    TreeNode* right;    int value;};bool hasPathSum(TreeNode* root, int sum){    if (root == nullptr)    {        return false;    }    if (root->left == nullptr && root->right == nullptr)    {        return sum == root->value;    }    return hasPathSum(root->left, sum - root->value)        || hasPathSum(root->right, sum - root->value);}

0 0