Beingamanforever

Code

Jul 3rd, 2024
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | Source Code | 0 0
  1. vector<vector<int>> pathsum(TreeNode* root, int val, int targetsum, vector<int> &current, vector<vector<int>> &ans) {
  2.     if (root == nullptr) return ans;
  3.  
  4.     val += root->val;
  5.     current.push_back(root->val);
  6.  
  7.     if (root->left == nullptr && root->right == nullptr && targetsum == val) {
  8.         ans.push_back(current);
  9.     } else {
  10.         pathsum(root->left, val, targetsum, current, ans);
  11.         pathsum(root->right, val, targetsum, current, ans);
  12.     }
  13.  
  14.     current.pop_back(); // Backtrack
  15.     return ans;
  16. }
  17.  
  18. vector<vector<int>> Solution::pathSum(TreeNode* A, int B) {
  19.     vector<vector<int>> ans;
  20.     vector<int> current;
  21.     pathsum(A, 0, B, current, ans);
  22.     return ans;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment