Skip to content

Commit 7edf759

Browse files
authored
Merge pull request doocs#322 from Stackingrule/dev
feat:add Solution.cpp for 0113. Path Sum II
2 parents 1ac6982 + 028d1b8 commit 7edf759

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> pathSum(TreeNode* root, int sum) {
4+
vector<vector<int>> res;
5+
vector<int> out;
6+
helper(root, sum, out, res);
7+
return res;
8+
}
9+
10+
private:
11+
void helper(TreeNode *node, int sum, vector<int> &out, vector<vector<int>> &res) {
12+
if (!node) return;
13+
out.push_back(node->val);
14+
if (sum == node->val && !node->left && !node->right) {
15+
res.push_back(out);
16+
}
17+
helper(node->left, sum - node->val, out, res);
18+
helper(node->right, sum - node->val, out, res);
19+
out.pop_back();
20+
}
21+
};

0 commit comments

Comments
 (0)