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;
}
找出二叉树所有根到叶子的路径,使得路径和为某一个指定的数字
最新推荐文章于 2018-06-05 10:44:14 发布