forked from coderchen/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathSum2.cpp
50 lines (41 loc) · 986 Bytes
/
PathSum2.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
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//----------------------------------leetcode---------------------------------
class Solution {
typedef vector<int> PATH_TYPE;
typedef vector<PATH_TYPE> RETURN_TYPE;
public:
RETURN_TYPE pathSum(TreeNode *root, int sum) {
RETURN_TYPE ret;
PATH_TYPE pt;
_pathSum(root, sum, 0, ret, pt);
return ret;
}
private:
void _pathSum(TreeNode* node, int sum, int currSum, RETURN_TYPE& rt, PATH_TYPE& pt)
{
if (node == nullptr) return;
pt.push_back(node->val);
currSum += node->val;
if (currSum == sum
&& node->left == nullptr
&& node->right == nullptr)
{
rt.push_back(pt);
}
_pathSum(node->left, sum, currSum, rt, pt);
_pathSum(node->right, sum, currSum, rt, pt);
pt.pop_back();
}
};
//----------------------------------leetcode---------------------------------
int main()
{
return 0;
}