diff --git a/C++/Path-Sum-II.cpp b/C++/Path-Sum-II.cpp new file mode 100644 index 00000000..08ed269c --- /dev/null +++ b/C++/Path-Sum-II.cpp @@ -0,0 +1,37 @@ +//CPP DFS solution +//Time complexity O(V+E) +//Space Complexity O(V) +class Solution { +public: + void dfs(vector>&v, vectork,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> pathSum(TreeNode* root, int sum) { + vector>v; + vectork; + dfs(v,k,root,sum); + return v; + } +}; \ No newline at end of file diff --git a/README.md b/README.md index 21065b01..31c06d08 100644 --- a/README.md +++ b/README.md @@ -357,8 +357,10 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 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 | | 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 | | 934 | [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) | [C++](./C++/Shortest-Bridge.cpp) | _O(V)_ | _O(V)_ | Medium | Graph | DFS + BFS | -| 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 | -| 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 | +| 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 | +| 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 | +| 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 | +