-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path113_PathSumII.cpp
43 lines (40 loc) · 997 Bytes
/
113_PathSumII.cpp
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
37
38
39
40
41
42
43
/*
* @Author: [email protected]
* @Last Modified time: 2016-04-19 10:25:40
*/
/**
* 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>> pathList;
vector<int> path;
findPath(root, sum, path, pathList);
return pathList;
}
void findPath(TreeNode* root, int sum, vector<int> path, vector<vector<int>> &pathList){
if(root==NULL) return;
path.push_back(root->val);
if(root->left == NULL && root->right==NULL && root->val==sum){
pathList.push_back(path);
}
findPath(root->left, sum-root->val, path, pathList);
findPath(root->right, sum-root->val, path, pathList);
path.pop_back();
}
};
/*
[]
0
[1,2,3,4,null,6,7,5,8]
15
[1,2,2,3,3,3,3]
6
*/