Skip to content

Commit 559d683

Browse files
Add path sum2 problem
1 parent fa49a23 commit 559d683

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

leetcode/medium/tree/path_sum_2.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://leetcode.com/problems/path-sum-ii/
2+
3+
/**
4+
* LOGIC: Basic tree problem... just know pass by reference
5+
*
6+
*/
7+
class Solution {
8+
public:
9+
void helper(TreeNode* root, int targetSum, vector<int> nodes, vector<vector<int>>& result) {
10+
if(!root) return;
11+
if(!root->left && !root->right) {
12+
if(targetSum - root->val==0) {
13+
nodes.push_back(root->val);
14+
result.push_back(nodes);
15+
}
16+
return;
17+
}
18+
19+
nodes.push_back(root->val);
20+
helper(root->left, targetSum - root->val, nodes, result);
21+
helper(root->right, targetSum - root->val, nodes, result);
22+
}
23+
24+
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
25+
vector<vector<int>> result;
26+
helper(root, targetSum, {}, result);
27+
return result;
28+
}
29+
};
30+

random_platform/misc/disjoint_sets/⭐️ rank_and_compression_algorithm_disjoint_set.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
* - While doing find, if we see there are more steps to reach parent from a node, we do compression
1414
* - Compression will just make sure that all child nodes directly point to parent rather than forming chain
1515
*
16-
*
17-
*
18-
*
19-
*
2016
*/
2117

2218
#include<iostream>

0 commit comments

Comments
 (0)