-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113.路径总和-ii.cpp
57 lines (50 loc) · 1.29 KB
/
113.路径总和-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
/*
* @lc app=leetcode.cn id=113 lang=cpp
*
* [113] 路径总和 II
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include <vector>
#include <deque>
class Solution {
public:
std::vector<std::vector<int>> pathSum(TreeNode* root, int sum) {
if(root == nullptr)
return {};
this->sum = sum;
DFS(root, 0, {});
return ret;
}
private:
std::vector<std::vector<int>> ret;
int sum;
void DFS(TreeNode* node, int currentSum, std::deque<int>&& r){
if(node->left == nullptr && node->right == nullptr){
if(node->val + currentSum == sum){
ret.emplace_back(r.begin(), r.end());
ret.back().emplace_back(node->val);
}
return;
}
if(node->left != nullptr){
r.emplace_back(node->val);
DFS(node->left, currentSum + node->val, std::move(r));
r.pop_back();
}
if(node->right != nullptr){
r.emplace_back(node->val);
DFS(node->right, currentSum + node->val, std::move(r));
r.pop_back();
}
}
};
// @lc code=end