backtobackswe / backtobackswe-feedback

1 stars 0 forks source link

All Paths with Sum equal to 'K' #9

Closed alan0428a closed 3 years ago

alan0428a commented 3 years ago

Back To Back SWE



Create a bug report to help us improve our content -



Your email registered on BackToBackSWE :

alan0428a@gmail.com



Category of the bug :



Description of the bug :



Code you used for Submit/Run operation :


class Solution {
  public:
    int pathSum(TreeNode* root, int sum) {
      unordered_map<int, int> map;
      map[0] = 1;

      return helper(root, sum, 0, map);
    }

    int helper(TreeNode *root, int target, int sum, unordered_map<int, int> &map)
    {
      if(!root) return 0;

      int count = 0;

      sum += root->value;

      if(map.find(sum - target) != map.end())
      {
        count += map[sum - target];
      }

      map[sum]++;

      count += helper(root->left, target, sum, map);
      count += helper(root->right, target, sum, map);

      map[sum]--;

      return count;
    }
};



Language used for code :

C++



Expected behavior :



Screenshots :



Additional context :



shubhankar-99 commented 3 years ago

Thank You for your contribution. We have added this solution on our website . You can find it at https://backtobackswe.com/platform/content/all-paths-with-sum/solutions