Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<vector<int>> pathsum(TreeNode* root, int val, int targetsum, vector<int> ¤t, vector<vector<int>> &ans) {
- if (root == nullptr) return ans;
- val += root->val;
- current.push_back(root->val);
- if (root->left == nullptr && root->right == nullptr && targetsum == val) {
- ans.push_back(current);
- } else {
- pathsum(root->left, val, targetsum, current, ans);
- pathsum(root->right, val, targetsum, current, ans);
- }
- current.pop_back(); // Backtrack
- return ans;
- }
- vector<vector<int>> Solution::pathSum(TreeNode* A, int B) {
- vector<vector<int>> ans;
- vector<int> current;
- pathsum(A, 0, B, current, ans);
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment