【发布时间】:2019-11-12 16:38:25
【问题描述】:
给定一棵二叉树,找到从根到叶的所有节点,当添加时等于目标总和。 该算法在 python 中使用时运行良好,但当我使用 c++ 时,它会引发错误。
错误:在第 22 行字符 80 处无效使用 void 表达式:
help(root->left, sum - root->val, temp.push_back(root->val), result);
这是我的代码。
void help(TreeNode* root, int sum, vector<int>& temp, vector<vector<int>>& result)
{
if ((sum == root->val) and (not root->left and not root->right))
{
temp.push_back(root->val);
result.push_back(temp);
return;
}
if (root->left){
help(root->left, sum - root->val, temp.push_back(root->val), result);
} // here i'm getting an error.
if (root->right){
help(root->right, sum - root->val, temp.push_back(root->val), result);
}
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if (root == NULL)
return;
vector<vector<int>> result;
vector<int> temp;
help(root, sum, temp, result);
return result;
}
我不明白如何解决此错误?
【问题讨论】:
标签: c++ binary-tree