-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path113.cpp
More file actions
28 lines (27 loc) · 764 Bytes
/
Copy path113.cpp
File metadata and controls
28 lines (27 loc) · 764 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
class Solution {
public:
vector<vector<int>> pathSum(TreeNode *root, int sum) {
vector<vector<int>> ret;
if (!root)
return ret;
vector<int> path;
helper(root, path, ret, sum);
return ret;
}
void helper(TreeNode *root, vector<int> &path, vector<vector<int>> &ret, int sum) {
if (!root)
return;
sum -= root->val;
path.push_back(root->val);
if (!root->left && !root->right && sum == 0) {
ret.push_back(path);
sum += root->val;
path.pop_back();
return;
}
helper(root->left, path, ret, sum);
helper(root->right, path, ret, sum);
sum += root->val;
path.pop_back();
}
};