-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113.cpp
41 lines (40 loc) · 1.08 KB
/
113.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
/**
* 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:
void solute(TreeNode* root, vector<vector<int> > &res, vector<int> local, int sum)
{
if(root->left == NULL && root->right == NULL)
{
local.push_back(root->val);
if(sum == root->val) res.push_back(local);
return;
}
if(root->left != NULL)
{
local.push_back(root->val);
solute(root->left, res, local, sum - root->val);
local.pop_back();
}
if(root->right != NULL)
{
local.push_back(root->val);
solute(root->right, res, local, sum - root->val);
local.pop_back();
}
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int> > res;
if(root == NULL) return res;
vector<int> local;
solute(root, res, local, sum);
return res;
}
};