-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path113.pathSum.cpp
More file actions
32 lines (28 loc) · 811 Bytes
/
113.pathSum.cpp
File metadata and controls
32 lines (28 loc) · 811 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
/**
* 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>> ans;
void dfs(TreeNode* root, int sum, vector<int> path) {
if (root == NULL) return;
sum -= root->val;
path.push_back(root->val);
if (root->left == NULL && root->right == NULL && sum == 0) {
ans.push_back(path);
}
if (root->left != NULL) dfs(root->left, sum, path);
if (root->right != NULL) dfs(root->right, sum, path);
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if (root == NULL) return ans;
dfs(root, sum, vector<int>());
return ans;
}
};