forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath-Sum-II.cpp
37 lines (31 loc) · 825 Bytes
/
Path-Sum-II.cpp
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
//CPP DFS solution
//Time complexity O(V+E)
//Space Complexity O(V)
class Solution {
public:
void dfs(vector<vector<int>>&v, vector<int>k,TreeNode*r,int s){
if(r==NULL){
return;
}
if(s==r->val&&r->left==NULL&&r->right==NULL){
k.push_back(r->val);
v.push_back(k);
//k.clear();
return;
}
else if(s!=r->val&&r->left==NULL&&r->right==NULL){
return;
}
k.push_back(r->val);
s-=r->val;
dfs(v,k,r->left,s);
dfs(v,k,r->right,s);
return;
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>>v;
vector<int>k;
dfs(v,k,root,sum);
return v;
}
};