-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_437_PathSumIII.cpp
62 lines (54 loc) · 1.45 KB
/
LC_437_PathSumIII.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
/*
https://leetcode.com/problems/path-sum-iii/
437. Path Sum III
https://practice.geeksforgeeks.org/problems/k-sum-paths/1
*/
/**
* 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 {
public:
typedef long ll;
unordered_map<ll, ll> um;
ll ans=0;
int ts=0;
void count(TreeNode* root, ll csum)
{
if(!root)
return;
csum += root->val;
ll rem = csum - ts;
if(um.find(rem) != um.end())
ans+= um[rem];
um[csum]++;
count(root->left, csum);
count(root->right, csum);
um[csum]--;
}
int pathSum(TreeNode* root, int targetSum) {
if(!root)
return 0;
// return solve(root, targetSum) + pathSum(root->left, targetSum) + pathSum(root->right, targetSum);
ts = targetSum;
um[0] = 1;
count(root, 0);
return ans;
}
long solve(TreeNode* root, long ts)
{
if(!root) return 0;
long cnt=0;
if(ts == root->val)
cnt++;
ts-=root->val;
return cnt+solve(root->left, ts) + solve(root->right, ts);
}
};