feat: 113_path_sum_2
This commit is contained in:
parent
b883e27508
commit
25a900bba3
41
113_path_sum_2/main.cpp
Normal file
41
113_path_sum_2/main.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user