-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumII.cpp
More file actions
36 lines (35 loc) · 907 Bytes
/
Copy pathPathSumII.cpp
File metadata and controls
36 lines (35 loc) · 907 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
36
/**
* Definition for binary tree
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > r;
vector<vector<int> > rightPath;
if (!root)
return r;
if (!root->left && !root->right) {
if (sum == root->val) {
vector<int> path;
path.push_back(sum);
r.push_back(path);
}
return r;
}
r = pathSum(root->left, sum - root->val);
rightPath = pathSum(root->right, sum - root->val);
r.insert(r.end(), rightPath.begin(), rightPath.end());
for (int i = 0; i < r.size(); i++) {
r[i].insert(r[i].begin(), root->val);
}
return r;
}
};