-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath-sum-iii.cpp
More file actions
52 lines (39 loc) · 1.25 KB
/
path-sum-iii.cpp
File metadata and controls
52 lines (39 loc) · 1.25 KB
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// https://www.cnblogs.com/grandyang/p/6007336.html
// um记录sum和path条数的对应关系
// 还是前序和的思想
int pathSum(TreeNode* root, int sum) {
unordered_map<int,int> um;
um[0]=1;
return helper(root,0,sum,um);
}
int helper(TreeNode* root, const int &presum, const int &sum, unordered_map<int,int> &um){
if(!root) return 0;
int currentsum=presum+root->val;
int result=um[currentsum-sum]; // 当前节点作为尾节点,符合的路径数目
um[currentsum]++;
result+=helper(root->left,currentsum,sum,um);
result+=helper(root->right,currentsum,sum,um);
um[currentsum]--;
return result;
}
// 这个解法我没有看得很明白:
// https://leetcode.com/problems/path-sum-iii/discuss/91889/Simple-Java-DFS
// https://www.cnblogs.com/grandyang/p/6007336.html 解法三
};
/*
1
-2 -3
1 3 -2
-1
*/