-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path113.PathSumII.cpp
More file actions
58 lines (48 loc) · 2.11 KB
/
113.PathSumII.cpp
File metadata and controls
58 lines (48 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
class Solution {
//Try commenting struct TreeNode out, this TreeNode* was customly declared to make my local machine compiler work.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
public:
vector<vector<int>>res;
vector<int>curr;
//custom 1d and 2d array declared
void dfs(TreeNode* root,int current_sum,int targetSum)
{
// actual working function that is required to make the traversal
if(root==NULL)
{ //if the root is empty it returns nothing and stops recursion
return;
}
curr.push_back(root->val);
//curr vector takes the val of the current root
current_sum+=root->val;
//current_sum var takes the sum of the val of the current root
if(root->left==NULL && root->right==NULL)
{//if the left and right child of the root of the TreeNode is empty then current sum is compared with targetSum
if(current_sum==targetSum)
{//if same then we reach our end where the sum is equal and we store the curr 1d vector into the 2d vector
res.push_back(curr);
}
}
dfs(root->left,current_sum,targetSum);
//dfs preorder traversal for the left side of the root [recursion]
dfs(root->right,current_sum,targetSum);
//dfs preorder traversal for the right side of the root [recursion]
curr.pop_back();
//removing the rest of the element if they are not equal to the targetSum after left and right traverse of the root nodes
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
//main function where dfs is called
dfs(root,0,targetSum);
//returning the 2d array having the respective collection of 1d arrays having the elements whose sum is equal to targetSum
return res;
}
};