Closed alan0428a closed 3 years ago
alan0428a@gmail.com
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; } };
C++
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
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 :
Language used for code :
C++
Expected behavior :
Screenshots :
Additional context :