-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0113.cpp
More file actions
26 lines (25 loc) · 802 Bytes
/
Copy path0113.cpp
File metadata and controls
26 lines (25 loc) · 802 Bytes
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
class Solution {
public:
vector<vector<int>> pathSum(TreeNode *root, int targetSum) {
if (!root) return {};
stack<pair<TreeNode *, int>> st;
vector<vector<int>> res;
vector<int> path;
st.push({root, 0});
while (!st.empty()) {
auto [root, sum] = st.top();
if (sum == INT_MIN) {
st.pop();
path.pop_back();
continue;
}
sum += root->val;
st.top().second = INT_MIN;
path.push_back(root->val);
if (!root->left && !root->right && sum == targetSum) res.push_back(path);
if (root->left) st.push({root->left, sum});
if (root->right) st.push({root->right, sum});
}
return res;
}
};