You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 810 / \ <b>5</b> <b>-3</b>
/ </b> </b> 3 2 11 / \ </b> 3 -2 1
Return 3. The paths that sum to 8 are:
- 5 -> 3
- 5 -> 2 -> 1
- -3 -> 11
Related Topics:
Tree
Similar Questions:
// OJ: https://leetcode.com/problems/path-sum-iii/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N^2)
class Solution {
int ans = 0;
unordered_map<int, int> dfs(TreeNode *root, int sum) { // map of sums from the current node to a child node
if (!root) return {};
unordered_map<int, int> m{{root->val, 1}};
auto left = dfs(root->left, sum), right = dfs(root->right, sum);
for (auto &[val, cnt] : left) m[val + root->val] += cnt;
for (auto &[val, cnt] : right) m[val + root->val] += cnt;
if (m.count(sum)) ans += m[sum];
return m;
}
public:
int pathSum(TreeNode* root, int sum) {
dfs(root, sum);
return ans;
}
};
// OJ: https://leetcode.com/problems/path-sum-iii/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(H)
class Solution {
private:
int cnt = 0;
void pathSum(TreeNode* root, int target, int sum) {
if (!root) return;
sum += root->val;
if (sum == target) cnt++;
pathSum(root->left, target, sum);
pathSum(root->right, target, sum);
}
public:
int pathSum(TreeNode* root, int sum) {
if (!root) return 0;
pathSum(root, sum, 0);
pathSum(root->left, sum);
pathSum(root->right, sum);
return cnt;
}
};
Or
// OJ: https://leetcode.com/problems/path-sum-iii/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(H)
class Solution {
private:
int pathSum(TreeNode* root, int target, int sum) { // count the paths starting from an acestor node to the current node whose sum equals `target`.
return root ? ((sum += root->val) == target) + pathSum(root->left, target, sum) + pathSum(root->right, target, sum) : 0;
}
public:
int pathSum(TreeNode* root, int sum) {
return root ? pathSum(root, sum, 0) + pathSum(root->left, sum) + pathSum(root->right, sum) : 0;
}
}
// OJ: https://leetcode.com/problems/path-sum-iii/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
// Ref: https://leetcode.com/problems/path-sum-iii/discuss/91878/17-ms-O(n)-java-Prefix-sum-method
class Solution {
unordered_map<int, int> m; // map from the path sum (from root to the current node) to the corresponding count
int dfs(TreeNode *root, int target, int sum) {
if (!root) return 0;
sum += root->val;
int ans = m.count(sum - target) ? m[sum - target] : 0;
m[sum]++;
ans += dfs(root->left, target, sum) + dfs(root->right, target, sum);
if (--m[sum] == 0) m.erase(sum);
return ans;
}
public:
int pathSum(TreeNode* root, int sum) {
m[0] = 1;
return dfs(root, sum, 0);
}
};