-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem437.cpp
40 lines (35 loc) · 1.48 KB
/
problem437.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
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
// ** explaintion
// first approach:
// 1)mplementtwo depth-first searches (DFS) to calculate the number of paths that equal a target sum. The first DFS iterates through each node (subtree), while the second DFS starts from each of these nodes (as root) to compute the paths
class Solution {
public:
int count = 0;
int calculatePaths(TreeNode*root, long long tar){
if(!root)return 0;
int counter = 0;
if(tar == root->val)counter = 1;
// This statement proposes subtracting the current node's value from the target sum to ascertain if the remaining value of targetsum equals the current node's value
tar -= root->val;
counter += calculatePaths(root->left, tar);
counter += calculatePaths(root->right, tar);
return counter;
}
int pathSum(TreeNode* root, int tar) {
if(!root)return 0;
count += calculatePaths(root, tar);
pathSum(root->left, tar);
pathSum(root->right, tar);
return count;
}
};