forked from LeetCode-in-Cpp/LeetCode-in-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
47 lines (43 loc) · 1.23 KB
/
Solution.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
// #Medium #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree #Big_O_Time_O(n)_Space_O(n)
// #2024_05_22_Time_15_ms_(70.45%)_Space_17.4_MB_(87.83%)
#include <cstddef>
/**
* 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) {}
* };
*/
class Solution {
private:
int count = 0;
public:
int pathSum(TreeNode* root, int targetSum) {
if (root == NULL) {
return 0;
}
helper(root, targetSum, 0);
pathSum(root->left, targetSum);
pathSum(root->right, targetSum);
return count;
}
void helper(TreeNode* node, int targetSum, long currSum) {
if (node == NULL) {
return;
}
currSum += node->val;
if (targetSum == currSum) {
count++;
}
if (node->left != NULL) {
helper(node->left, targetSum, currSum);
}
if (node->right != NULL) {
helper(node->right, targetSum, currSum);
}
}
};