Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emahtab/path-sum-iii
https://github.com/emahtab/path-sum-iii
leetcode path-sum prefix-sum recursion
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/path-sum-iii
- Owner: eMahtab
- Created: 2021-11-26T15:28:58.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-18T04:28:16.000Z (3 months ago)
- Last Synced: 2024-10-20T14:52:58.713Z (3 months ago)
- Topics: leetcode, path-sum, prefix-sum, recursion
- Homepage:
- Size: 49.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Path Sum III
## https://leetcode.com/problems/path-sum-iii
Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
![Path Sum III](example.JPG?raw=true)
# Implementation 1a : O(n^2)
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int pathSum(TreeNode root, int sum) {
if(root == null)
return 0;
return pathSum(root.left, sum) + pathSum(root.right, sum) + findPath(root, sum);
}public int findPath(TreeNode node, int target){
int numberOfPathFound = 0;
if(node.val == target)
numberOfPathFound++;
if(node.left != null)
numberOfPathFound += findPath(node.left, target - node.val);
if(node.right != null)
numberOfPathFound += findPath(node.right, target - node.val);
return numberOfPathFound;
}
}
```## Implementation 1b : O(n^2) Making leetcode happy (when node.val is very large, target-node.val can lead to integer overflow)
```java
class Solution {
public int pathSum(TreeNode root, int sum) {
if(root == null)
return 0;
return pathSum(root.left, sum) + pathSum(root.right, sum) + findPath(root, sum);
}public int findPath(TreeNode node, long target){
int numberOfPathFound = 0;
if(node.val == target)
numberOfPathFound++;
if(node.left != null)
numberOfPathFound += findPath(node.left, target - node.val);
if(node.right != null)
numberOfPathFound += findPath(node.right, target - node.val);
return numberOfPathFound;
}
}
```# Implementation 2 : O(n) Using Prefix Sum
```java
class Solution {
public int pathSum(TreeNode root, int targetSum) {
Map map = new HashMap<>();
map.put(0L, 1);
return helper(root, (long) targetSum, 0, map);
}private int helper(TreeNode node, long targetSum, long prefixSum, Map map) {
if (node == null) {
return 0;
}
prefixSum += node.val;
int count = map.getOrDefault(prefixSum - targetSum, 0);
map.put(prefixSum, map.getOrDefault(prefixSum, 0) + 1);
count += helper(node.left, targetSum, prefixSum, map) + helper(node.right, targetSum, prefixSum, map);
map.put(prefixSum, map.get(prefixSum) - 1);
return count;
}
}
```# References :
1. https://www.youtube.com/watch?v=uZzvivFkgtM
2. https://github.com/ojasmaru/LetsAlgoTogether/blob/master/Path%20Sum%20III/Java/QuickStart.java