leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0113.cpp (802B)
0 class Solution {
1 public:
2 vector<vector<int>> pathSum(TreeNode *root, int targetSum) {
3 if (!root) return {};
4 stack<pair<TreeNode *, int>> st;
5 vector<vector<int>> res;
6 vector<int> path;
7 st.push({root, 0});
8 while (!st.empty()) {
9 auto [root, sum] = st.top();
10 if (sum == INT_MIN) {
11 st.pop();
12 path.pop_back();
13 continue;
14 }
15 sum += root->val;
16 st.top().second = INT_MIN;
17 path.push_back(root->val);
19 if (!root->left && !root->right && sum == targetSum) res.push_back(path);
20 if (root->left) st.push({root->left, sum});
21 if (root->right) st.push({root->right, sum});
22 }
23 return res;
24 }
25 };