Skip to content

Commit 51d9bbb

Browse files
Solved Path Sum II(LeetCode 113(https://leetcode.com/problems/path-sum-ii/) | Graph | Medium) and updated Readme.md (#104)
Co-authored-by: Gourav Rusiya <[email protected]>
1 parent ca025a3 commit 51d9bbb

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

C++/Path-Sum-II.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//CPP DFS solution
2+
//Time complexity O(V+E)
3+
//Space Complexity O(V)
4+
class Solution {
5+
public:
6+
void dfs(vector<vector<int>>&v, vector<int>k,TreeNode*r,int s){
7+
if(r==NULL){
8+
return;
9+
}
10+
if(s==r->val&&r->left==NULL&&r->right==NULL){
11+
k.push_back(r->val);
12+
v.push_back(k);
13+
//k.clear();
14+
return;
15+
16+
}
17+
else if(s!=r->val&&r->left==NULL&&r->right==NULL){
18+
return;
19+
}
20+
21+
k.push_back(r->val);
22+
s-=r->val;
23+
dfs(v,k,r->left,s);
24+
dfs(v,k,r->right,s);
25+
26+
27+
28+
return;
29+
30+
}
31+
vector<vector<int>> pathSum(TreeNode* root, int sum) {
32+
vector<vector<int>>v;
33+
vector<int>k;
34+
dfs(v,k,root,sum);
35+
return v;
36+
}
37+
};

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,10 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
357357
| 1042 | [Flower Planting with No Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent/) | [Python](./Python/1042_FlowerPlantingwithNoAdjacent.py) | _O(V+E)_ | _O(2V+E)_ | Medium | Graph | Graph Coloring |
358358
| 797 | [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target/) | [Java](./Java/All_Paths_From_Source_to_Target.java) | _O(N^2)_ | _O(N)_ | Medium | Graph | DFS |
359359
| 934 | [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) | [C++](./C++/Shortest-Bridge.cpp) | _O(V)_ | _O(V)_ | Medium | Graph | DFS + BFS |
360-
| 1192 | [Critical Connections in a Network](https://leetcode.com/problems/critical-connections-in-a-network/) | [C++](./C++/Critical-Connections-in-a-Network.cpp) | _O(V+E)_ | _O(4V+E)_ | Hard | Graph | Tarjan's Algorithm |
361-
| 785 | [Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/) | [C++](./C++/Is-Graph-Bipartite.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | BFS |
360+
| 1192 | [Critical Connections in a Network](https://leetcode.com/problems/critical-connections-in-a-network/) | [C++](./C++/Critical-Connections-in-a-Network.cpp) | _O(V+E)_ | _O(4V+E)_ | Hard | Graph | Tarjan's Algorithm |
361+
| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [C++](./C++/Critical-Path-Sum-II.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | DFS |
362+
| 785 | [Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/) | [C++](./C++/Is-Graph-Bipartite.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | BFS |
363+
362364
<br/>
363365
<div align="right">
364366
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)