forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0113.cpp
More file actions
35 lines (32 loc) · 893 Bytes
/
0113.cpp
File metadata and controls
35 lines (32 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> ans;
vector<int> curr;
dfs(root, sum, curr, ans);
return ans;
}
private:
void dfs(TreeNode* root, int sum, vector<int>& curr, vector<vector<int>>& ans) {
if (!root) return;
if (sum == root->val && !root->left && !root->right) {
curr.push_back(root->val);
ans.push_back(curr);
curr.pop_back();
return;
}
curr.push_back(root->val);
dfs(root->left, sum - root->val, curr, ans);
dfs(root->right, sum - root->val, curr, ans);
curr.pop_back();
}
};