-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0113 - Path Sum II.cpp
62 lines (49 loc) · 1.59 KB
/
0113 - Path Sum II.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
#include <vector>
// Definition for a binary tree node.
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) {}
};
using vecvec = std::vector<std::vector<int>>;
class Solution
{
public:
vecvec pathSumH(TreeNode *root, int target_sum, int depth)
{
if (!root) return {};
target_sum -= root->val;
if (!root->left && !root->right) // leaf
{
if (target_sum) return {};
else
{
vecvec res(1);
res[0].reserve(depth);
res[0].push_back(root->val);
return std::move(res);
}
}
vecvec left = pathSumH(root->left, target_sum, depth + 1);
if (left.size())
for (auto & vec : left) vec.push_back(root->val);
vecvec right = pathSumH(root->right, target_sum, depth + 1);
if (right.size())
for (auto & vec : right) vec.push_back(root->val);
if (!left.size() && right.size()) std::swap(left, right);
left.reserve(left.size() + right.size());
for (auto & vec : right) left.push_back(vec);
return std::move(left);
}
vecvec pathSum(TreeNode *root, int target_sum)
{
vecvec res = pathSumH(root, target_sum, 1);
for (auto & vec : res)
std::reverse(vec.begin(), vec.end());
return std::move(res);
}
};