feat: 113_path_sum_2

This commit is contained in:
SquidSpirit 2025-03-12 23:57:54 +08:00
parent b883e27508
commit 25a900bba3

41
113_path_sum_2/main.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<vector<int>> pathSum(TreeNode *root, int targetSum) {
vector<vector<int>> result;
unordered_map<TreeNode *, bool> visited;
function<void(TreeNode *, int, vector<int>)> dfs = [&](TreeNode *root, int currentSum, vector<int> path) {
if (!root) return;
if (visited[root]) return;
visited[root] = true;
path.emplace_back(root->val);
currentSum += root->val;
if (!root->left && !root->right) {
if (currentSum == targetSum) {
result.emplace_back(path);
}
}
dfs(root->left, currentSum, path);
dfs(root->right, currentSum, path);
};
dfs(root, 0, {});
return result;
}
};