-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumII.cpp
More file actions
57 lines (48 loc) · 1.58 KB
/
PathSumII.cpp
File metadata and controls
57 lines (48 loc) · 1.58 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
51
52
53
54
55
56
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > paths;
vector<TreeNode *> path_nodes;
stack<TreeNode *> travel_stack;
if (root == NULL) return paths;
travel_stack.push(root);
while (!travel_stack.empty()) {
TreeNode *node = travel_stack.top();
sum = sum - node->val;
path_nodes.push_back(node);
travel_stack.pop();
if (node->left) travel_stack.push(node->left);
if (node->right) travel_stack.push(node->right);
//find one path
if (!node->left && !node->right && !sum) {
vector<int> path;
for (auto p : path_nodes) path.push_back(p->val);
paths.push_back(path);
}
//roll back
while (!path_nodes.empty()) {
if (travel_stack.empty()) break;
if (
(path_nodes.back())->left == travel_stack.top() ||
(path_nodes.back())->right == travel_stack.top()
)break;
else {
sum = sum + (path_nodes.back())->val;
path_nodes.pop_back();
}
}
}
return paths;
}
};