-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113. Path Sum II.cpp
92 lines (66 loc) · 2.01 KB
/
113. 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<vector>
#include<iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
void depthFirstSearch(TreeNode* node, int currentSum, vector<int> path){
currentSum = currentSum + node->val;
path.push_back(node->val);
if(node->left == nullptr && node->right == nullptr){
if(currentSum == target){
result.push_back(path);
}
return;
}
if(node->left){
depthFirstSearch(node->left,currentSum,path);
}
if(node->right){
depthFirstSearch(node->right,currentSum,path);
}
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if(!root){
return result;
}
target = targetSum;
depthFirstSearch(root,0,{});
return result;
}
private:
int target;
vector<vector<int>> result;
};
int main(){
Solution solution;
TreeNode* root = new TreeNode(5);
TreeNode* n2 = new TreeNode(4);
TreeNode* n3 = new TreeNode(8);
TreeNode* n4 = new TreeNode(11);
TreeNode* n5 = new TreeNode(7);
TreeNode* n6 = new TreeNode(2);
TreeNode* n7 = new TreeNode(13);
TreeNode* n8 = new TreeNode(4);
TreeNode* n9 = new TreeNode(5);
TreeNode* n10 = new TreeNode(1);
root->left = n2;
root->right = n3;
n2->left = n4;
n4->left = n5;
n4->right = n6;
n3->left = n7;
n3->right = n8;
n8->left = n9;
n8->right = n10;
vector<vector<int>> answer = solution.pathSum(root,22);
cout << "Size: " << answer.size() << endl;
return 0;
}