Video solution | Prefix sum | Intuition explained in detail | C++
Video\nHey everyone, i have created a video solution for this problem where we discuss intuition for this problem, this video is part of my trees playlist and b
# Video\nHey everyone, i have created a video solution for this problem where we discuss intuition for this problem, this video is part of my trees playlist and belongs to category "Root to leaf path".\nI solve this i have used the approach of prefix sum.\n\nVideo link: https://youtu.be/xdUIdYN01Rg\nPlaylist link: https://www.youtube.com/playlist?list=PLICVjZ3X1Aca0TjUTcsD7mobU001CE7GL\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    unordered_map<long , int> mp;\n    int count=0;\n    void dfs(TreeNode* root, int targetSum,long prefix_sum){\n        if(!root) return ;\n        prefix_sum+=root->val;\n        if(mp.find(prefix_sum-targetSum)!=mp.end()){\n            count+=mp[prefix_sum-targetSum];\n        }\n        mp[prefix_sum]++;\n        dfs(root->left, targetSum, prefix_sum);\n        dfs(root->right, targetSum, prefix_sum);\n        mp[prefix_sum]--;\n    }\n    int pathSum(TreeNode* root, int targetSum) {\n        mp[0]=1;\n        dfs(root, targetSum,0);\n        return count;\n        \n    }\n};\n```
['C++']