-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleet113.cpp
93 lines (88 loc) · 2.1 KB
/
leet113.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <functional>
#include <cmath>
#include <set>
#include <map>
#include <assert.h>
#include <stack>
#include <unordered_map>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
/**
* 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> > pathSum(TreeNode* root, int sum) {
vector<vector<int> > re;
vector<vector<int> > t = get(root);
for(int i=0;i<t.size();i++)
{
vector<int> v = t[i];
if(accumulate(v.begin(),v.end(),0)==sum)
re.push_back(v);
}
return re;
}
vector<vector<int> > get(TreeNode* root)
{
vector<vector<int> > re;
if(root==NULL)
return re;
int val = root->val;
if(root->left==NULL && root->right==NULL)
{
vector<int> t;
t.push_back(val);
re.push_back(t);
return re;
}
vector<vector<int> > left = get(root->left);
vector<vector<int> > right = get(root->right);
for(int i=0;i<left.size();i++)
{
vector<int> v = left[i];
vector<int> temp;
temp.push_back(val);
for(int j=0;j<v.size();j++)
{
temp.push_back(v[j]);
}
re.push_back(temp);
}
for(int i=0;i<right.size();i++)
{
vector<int> v = right[i];
vector<int> temp;
temp.push_back(val);
for(int j=0;j<v.size();j++)
{
temp.push_back(v[j]);
}
re.push_back(temp);
}
return re;
}
};