-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113.path_sum_II.cpp
More file actions
50 lines (44 loc) · 1.22 KB
/
Copy path113.path_sum_II.cpp
File metadata and controls
50 lines (44 loc) · 1.22 KB
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
//coding:utf-8
/***********************************************************
Program: Path Sum II
Description:
Shanbo Cheng: cshanbo@gmail.com
Date: 2016-08-11 16:14:09
Last modified: 2016-08-19 08:47:25
GCC version: 4.9.3
***********************************************************/
//A basic dfs solution, no other tricks
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
template <typename T>
using matrix = vector<vector<T>>;
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
matrix<int> ret;
if(!root)
return ret;
vector<int> one;
helper(root, ret, one, sum);
return ret;
}
void helper(TreeNode* node, matrix<int>& ret, vector<int> one, int val) {
if(!node)
return;
if(node && !node->left && !node->right && node->val == val) {
one.push_back(val);
ret.push_back(one);
return;
}
one.push_back(node->val);
helper(node->left, ret, one, val - node->val);
helper(node->right, ret, one, val - node->val);
return;
}
};