阅读背景:

找出二叉树所有根到叶子的路径,使得路径和为某一个指定的数字

来源:互联网 
struct TreeNode
{
    TreeNode* left;
    TreeNode* right;
    int value;
};

void pathSum(TreeNode* root, int gap, vector<int>& cur, vector< vector<int> >& res)
{
    if (root == nullptr)
    {
        return;
    }
    cur.push_back(root->value);
    if (root->left == nullptr && root->right == nullptr)
    {
        if (gap == root->value)
        {
            res.push_back(cur);
        }
    }
    pathSum(root->left, gap - root->value, cur, res);
    pathSum(root->right, gap - root->value, cur, res);
    cur.pop_back();
}

vector< vector<int> > pathSum(TreeNode* root, int sum)
{
    vector< vector<int> > res;
    vector<int> cur;
    pathSum(root, sum, cur, res);
    return res;
}struct TreeNode
{
    TreeNode* left;
    TreeN



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: