Skip to content

Commit d083d71

Browse files
committed
437_Path_Sum_III
1 parent 65ada08 commit d083d71

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Remember solutions are only solutions to given problems. If you want full study
130130
| 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/421_Maximum_XOR_of_Two_Numbers_in_an_Array.py) | Check 0~32 prefix, check if there is x y in prefixes, where x ^ y = answer ^ 1, O(32n) and O(n) |
131131
| 422 | [Valid Word Square](https://leetcode.com/problems/valid-word-square/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/422_Valid_Word_Square.py) | Compare row with column, O(n^2) |
132132
| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/434_Number_of_Segments_in_a_String.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/434_Number_of_Segments_in_a_String.java) | 1. trim &split<br>2. Find segment in place |
133+
| 437 | [Path Sum III](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/437_Path_Sum_III.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/437_Path_Sum_III.java) | 1. Recursively travese the whole tree, O(n^2)<br>2. Cache sum in Hash based on 1.|
133134
| 438 | [Find All Anagrams in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/438_Find_All_Anagrams_in_a_String.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/438_Find_All_Anagrams_in_a_String.java) | Build a char count list with 26-256 length. Note that this list can be update when going through the string. O(n) and O(1) |
134135
| 443 | [String Compression](https://leetcode.com/problems/string-compression/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/443_String_Compression.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/443_String_Compression.java) | Maintain curr, read, write and anchor (start of this char). |
135136
| 453 | [Number of Segments in a String](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/453_Minimum_Moves_to_Equal_Array_Elements.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/453_Minimum_Moves_to_Equal_Array_Elements.java) | Each move is equal to minus one element in array, so the answer is the sum of all elements after minus min. |

java/437_Path_Sum_III.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
/*public int pathSum(TreeNode root, int sum) {
12+
if (root != null) return findPaths(root, sum)
13+
+ pathSum(root.left, sum)
14+
+ pathSum(root.right, sum);
15+
return 0;
16+
}
17+
18+
private int findPaths(TreeNode root, int target) {
19+
if (root != null) return (root.val == target? 1 : 0)
20+
+ findPaths(root.left, target - root.val)
21+
+ findPaths(root.right, target - root.val);
22+
return 0;
23+
}*/
24+
25+
private int result;
26+
private HashMap<Integer, Integer> cache;
27+
28+
public int pathSum(TreeNode root, int sum) {
29+
result = 0;
30+
cache = new HashMap<Integer, Integer>();
31+
cache.put(0, 1);
32+
pathSumHelper(root, sum, 0);
33+
return result;
34+
}
35+
// https://leetcode.com/problems/path-sum-iii/discuss/91892/Python-solution-with-detailed-explanation
36+
private void pathSumHelper(TreeNode root, int target, int soFar) {
37+
if (root != null) {
38+
int complement = soFar + root.val - target;
39+
if (cache.containsKey(complement))
40+
result += cache.get(complement);
41+
cache.put(soFar + root.val, cache.getOrDefault(soFar + root.val, 0) + 1);
42+
pathSumHelper(root.left, target, soFar + root.val);
43+
pathSumHelper(root.right, target, soFar + root.val);
44+
cache.put(soFar + root.val, cache.get(soFar + root.val) - 1);
45+
}
46+
}
47+
}

python/437_Path_Sum_III.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
9+
# https://leetcode.com/problems/path-sum-iii/discuss/91892/Python-solution-with-detailed-explanation
10+
class Solution(object):
11+
# def find_paths(self, root, target):
12+
# if root:
13+
# return int(root.val == target)
14+
# + self.find_paths(root.left, target - root.val)
15+
# + self.find_paths(root.right, target - root.val)
16+
# return 0
17+
18+
# def pathSum(self, root, sum):
19+
# """
20+
# :type root: TreeNode
21+
# :type sum: int
22+
# :rtype: int
23+
# """
24+
# if root:
25+
# return self.find_paths(root, sum)
26+
# + self.pathSum(root.left, sum)
27+
# + self.pathSum(root.right, sum)
28+
# return 0
29+
30+
def pathSumHelper(self, root, target, so_far, cache):
31+
if root:
32+
complement = so_far + root.val - target
33+
if complement in cache:
34+
self.result += cache[complement]
35+
cache[so_far + root.val] = cache.get(so_far + root.val, 0) + 1
36+
self.pathSumHelper(root.left, target, so_far + root.val, cache)
37+
self.pathSumHelper(root.right, target, so_far + root.val, cache)
38+
cache[so_far + root.val] -= 1
39+
return
40+
41+
def pathSum(self, root, sum):
42+
"""
43+
:type root: TreeNode
44+
:type sum: int
45+
:rtype: int
46+
"""
47+
self.result = 0
48+
self.pathSumHelper(root, sum, 0, {0: 1})
49+
return self.result

0 commit comments

Comments
 (0)