Leetcode 113 Solution
This article provides solution to leetcode question 113 (path-sum-ii)
Access this page by simply typing in "lcs 113" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/path-sum-ii
Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
vector<vector<int>> m_res;
public:
void pathSum(TreeNode* root, int sum, vector<int>& a, int curr) {
if (root == NULL)
return;
a.push_back(root->val);
if (root->left || root->right)
{
pathSum(root->left, sum, a, curr + root->val);
pathSum(root->right, sum, a, curr + root->val);
}
else if (curr + root->val == sum)
{
m_res.push_back(a);
}
a.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<int> a;
pathSum(root, sum, a, 0);
return m_res;
}
};