1
+ /*
2
+ written by Pankaj Kumar.
3
+ country:-INDIA
4
+ Institute: National Institute of Technology, Uttarakhand
5
+ */
6
+ typedef long long ll ;
7
+ typedef unsigned long long ull;
8
+ typedef vector<int > vl;
9
+ typedef vector<vector<int >> vvl;
10
+ #define speed cin.tie(0 );cout.tie(0 );ios_base::sync_with_stdio(0 );
11
+ /* Abbrevations */
12
+ #define ff first
13
+ #define ss second
14
+ #define mp make_pair
15
+ #define pb push_back
16
+ // loops
17
+ #define forin (arr,n ) for (ll i=0 ;i<n;i++) cin>>arr[i];
18
+ // Some print
19
+ #define no cout<<" NO" <<endl;
20
+ #define yes cout<<" YES" <<endl;
21
+ // sort
22
+ #define all (V ) (V).begin(),(V).end()
23
+ #define srt (V ) sort(all(V))
24
+ #define srtGreat (V ) sort(all(V),greater<ll>())
25
+ // some extra
26
+ #define printv (v ) for (ll i=0 ;i<ll(v.size());i++){cout<<v[i]<<" " ;} cout<<endl;
27
+ #define precision (x ) cout<<fixed<<setprecision(x);
28
+ #define sz (V ) ll(V.size())
29
+
30
+
31
+ /* ascii value
32
+ A=65,Z=90,a=97,z=122
33
+ */
34
+
35
+ /* Some syntax
36
+ //Syntax to create a min heap for priority queue
37
+ //priority_queue <int, vector<int>, greater<int>>pq;
38
+ */
39
+ /* --------------------MAIN PROGRAM----------------------------*/
40
+ // to run ctrl+b
41
+ const ll INF=1e18 ;
42
+ const ll mod1=1e9 +7 ;
43
+ const ll mod2=998244353 ;
44
+ // Techniques :
45
+ // divide into cases, brute force, pattern finding
46
+ // sort, greedy, binary search, two pointer
47
+ // transform into graph
48
+
49
+ // Experience :
50
+ // Cp is nothing but only observation and mathematics.
51
+
52
+
53
+ // Add main code here
54
+
55
+ /* *
56
+ * Definition for a binary tree node.
57
+ * struct TreeNode {
58
+ * int val;
59
+ * TreeNode *left;
60
+ * TreeNode *right;
61
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
62
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
63
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
64
+ * };
65
+ */
66
+ class Solution {
67
+ public:
68
+ vector<int > path;
69
+ vector<vector<int >> all_path;
70
+
71
+ void dfs (TreeNode* root,int targetSum,int & sum){
72
+ if (root==nullptr ){
73
+ return ;
74
+ }
75
+ sum+=root->val ;
76
+ path.pb (root->val );
77
+ if (root->left ==nullptr &&root->right ==nullptr &&sum==targetSum){
78
+ all_path.pb (path);
79
+ }
80
+ dfs (root->left ,targetSum,sum);
81
+ dfs (root->right ,targetSum,sum);
82
+ sum-=root->val ;
83
+ path.pop_back ();
84
+ return ;
85
+ }
86
+ vector<vector<int >> pathSum (TreeNode* root, int targetSum) {
87
+ int sum=0 ;
88
+ dfs (root,targetSum,sum);
89
+ return all_path;
90
+ }
91
+ };
92
+
93
+
94
+
95
+ /* -----------------END OF PROGRAM --------------------*/
96
+ /*
97
+ * stuff you should look before submission
98
+ * constraint and time limit
99
+ * int overflow
100
+ * special test case (n=0||n=1||n=2)
101
+ * don't get stuck on one approach if you get wrong answer
102
+ */
0 commit comments