From 7b6c8aca963e04fbee6900e7a255511ea3fa6899 Mon Sep 17 00:00:00 2001 From: MorviBhojwani Date: Sat, 17 Oct 2020 18:00:46 +0530 Subject: [PATCH] Solved Path Sum II --- C++/Path-Sum-II.cpp | 37 +++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 38 insertions(+) create mode 100644 C++/Path-Sum-II.cpp 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 cb53cd4b..89b1ab7b 100644 --- a/README.md +++ b/README.md @@ -358,6 +358,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 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 | | 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 |