-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113_path_sum.cc
53 lines (44 loc) · 1.01 KB
/
113_path_sum.cc
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
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdint-intn.h>
#include <c++/7/bits/c++config.h>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
#include <queue>
#include <stack>
using namespace::std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
void dfs(TreeNode* root, vector<int>& ans, int& target) {
if (!root)
return;
ans.push_back(root->val);
target -= root->val;
if (!root->left && !root->right && !target)
result.push_back(ans);
dfs(root->left, ans, target);
dfs(root->right, ans, target);
target += root->val;
ans.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<int> ans;
dfs(root, ans, sum);
return result;
}
private:
vector<vector<int>> result;
};
int main()
{
return 0;
}