MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / Solution

Class Solution

C++/Path-Sum-II.cpp:4–37  ·  view source on GitHub ↗

CPP DFS solution Time complexity O(V+E) Space Complexity O(V)

Source from the content-addressed store, hash-verified

2//Time complexity O(V+E)
3//Space Complexity O(V)
4class Solution {
5public:
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};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected