//剑指offer:搜索一个二叉树,并且把和为expectNumber的路径上的值输出
void funcPath(TreeNode* root, int expectNumber, int currentSum, vector<int> v, vector<vector<int> >& res) {
currentSum += root->val;
v.push_back(root->val);
if (currentSum == expectNumber && root->left == nullptr && root->right == nullptr) {
res.push_back(v);
}
if (root->left != nullptr)
funcPath(root->left, expectNumber, currentSum, v, res);
if (root->right != nullptr)
funcPath(root->right, expectNumber, currentSum, v, res);
v.pop_back();
}
剑指offer:输出二叉树和为某一值的所有路径
最新推荐文章于 2022-05-20 08:00:00 发布
本文介绍了一个算法问题,即在二叉树中寻找所有节点值之和等于给定值的路径,并给出了具体的实现代码。该算法递归地遍历二叉树的每个节点,跟踪当前路径的和。
614

被折叠的 条评论
为什么被折叠?



