-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
38 lines (34 loc) · 890 Bytes
/
solution.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
/**
* 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> > result;
void dfs(TreeNode *node, int sum, vector<int> curPath, int curSum)
{
if (node == NULL)
return;
curPath.push_back(node->val);
if (node->left == NULL && node->right == NULL)
{
if(curSum + node->val == sum)
result.push_back(curPath);
return;
}
dfs(node->left, sum, curPath, curSum + node->val);
dfs(node->right, sum, curPath, curSum + node->val);
}
vector<vector<int> > pathSum(TreeNode *root, int sum)
{
vector<int> curPath;
dfs(root,sum,curPath,0);
return result;
}
};