-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113-Path Sum II.cpp
More file actions
36 lines (36 loc) · 848 Bytes
/
113-Path Sum II.cpp
File metadata and controls
36 lines (36 loc) · 848 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 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>> result;
vector<int> temp;
void helper(TreeNode* root, int sum)
{
if(!root)
return;
temp.push_back(root->val);
if(root->val==sum&&!root->right&&!root->left)
{
result.push_back(temp);
temp.pop_back();
return;
}
helper(root->left,sum-root->val);
helper(root->right,sum-root->val);
temp.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum)
{
temp.reserve(1000);
result.reserve(1000);
helper(root,sum);
return result;
}
};