forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
30 lines (28 loc) · 848 Bytes
/
solution.h
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
#include <vector>
#include <cstddef>
#include <stack>
#include <algorithm>
using std::vector;
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>> ret;
vector<int> path;
pathSum(root, sum, path, ret);
return ret;
}
void pathSum(TreeNode *root, int sum, vector<int> &path, vector<vector<int>> &ret) {
if (!root) return;
path.push_back(root->val);
if (!root->left && !root->right && root->val == sum) ret.push_back(path);
if (root->left) pathSum(root->left, sum-root->val, path, ret);
if (root->right) pathSum(root->right, sum-root->val, path, ret);
path.pop_back();
}
};