Skip to content

Commit 40ccc5b

Browse files
🔥 Day 8
1 parent 1d0b647 commit 40ccc5b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
[Challenge Link](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/)
66

7-
## Week 1 🚧
7+
## Week 1
88
1. [Detect Capital](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3409/) ➡️ [CPP Solution](Week1/detectCapital.cpp)
99
2. [Design HashSet](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3410/) ➡️ [CPP Solution](Week1/designHashset.cpp)
1010
3. [Valid Palindrome](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3411/) ➡️ [CPP Solution](Week1/validPalindrome.cpp)
@@ -13,6 +13,9 @@
1313
6. [Find All Duplicates in an Array](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3414/) ➡️ [CPP Solution](Week1/findDuplicates.cpp)
1414
7. [Vertical Order Traversal of a Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3415/) ➡️ [CPP Solution](Week1/verticalTraversal.cpp)
1515

16+
## Week 2 🚧
17+
1. [Path Sum III](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3417/) ➡️ [CPP Solution](Week2/pathSum.cpp)
18+
1619
## Where to find me? 🌟
1720
* [Website](https://akashwho.codes/)
1821
* [Linkedin](https://www.linkedin.com/in/AkashRajpurohit)

Week2/pathSum.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
int pathSumHelper(TreeNode* root, int currSum, int target, unordered_map<int, int>& preSum) {
14+
if(root == NULL) return 0;
15+
16+
currSum += root->val;
17+
18+
int res = preSum[currSum - target];
19+
20+
preSum[currSum]++;
21+
22+
res += pathSumHelper(root->left, currSum, target, preSum) + pathSumHelper(root->right, currSum, target, preSum);
23+
24+
preSum[currSum]--;
25+
26+
return res;
27+
}
28+
public:
29+
int pathSum(TreeNode* root, int sum) {
30+
unordered_map<int, int> preSum;
31+
preSum[0] = 1;
32+
return pathSumHelper(root, 0, sum, preSum);
33+
}
34+
};

0 commit comments

Comments
 (0)