-
Notifications
You must be signed in to change notification settings - Fork 0
/
113.path-sum-ii.cpp
51 lines (48 loc) · 1.23 KB
/
113.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
/*
* @lc app=leetcode id=113 lang=cpp
*
* [113] Path Sum II
*/
// @lc code=start
/**
* 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) {}
* };
*/
class Solution {
void solve(
vector<vector<int>> &answer,
vector<int> &container,
TreeNode *root,
int rest
) {
if(!root) return;
container.push_back(root->val);
rest -= root->val;
if(!rest && !root->left && !root->right) {
answer.push_back(container);
} else {
solve(answer, container, root->left, rest);
solve(answer, container, root->right, rest);
}
container.pop_back();
}
public:
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> answer;
vector<int> temp;
solve(answer, temp, root, targetSum);
return move(answer);
}
};
// Accepted
// 115/115 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 81.42 % of cpp submissions (19.8 MB)
// @lc code=end