-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113.cpp
31 lines (30 loc) · 838 Bytes
/
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
// recursion-13ms.cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
vector<vector<int>> result;
void hasSum(TreeNode* root, int sum, vector<int>& now) {
if (root == nullptr)
return;
now.push_back(root->val);
if (0 == sum - root->val && root->left == nullptr &&
root->right == nullptr)
result.push_back(now);
hasSum(root->left, sum - root->val, now);
hasSum(root->right, sum - root->val, now);
now.pop_back();
}
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<int> now;
hasSum(root, sum, now);
return result;
}
};