class Solution
{
public:
vector<vector<int>> findpath(ListNode* root, int target)
{
vector<int> temp;
vector<vector<int>> res;
if (!root)
{
return res;
}
dfs(root, target, temp, res);
return res;
}
void dfs(ListNode* root, int target, vector<int>& temp, vector <vector<int >>& res)
{
temp.push_back(root->val);
if (!root->left && !root->right && target - root->val == 0)
{
res.push_back(temp);
}
if (root->left)
{
dfs(root->left, target - root->val, temp, res);
}
if (root->right)
{
dfs(root->right, target - root->val, temp, res);
}
}
};
二叉树和为某一值的路径
最新推荐文章于 2024-09-16 15:03:26 发布