-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy path437_Path_Sum_III.py
51 lines (46 loc) · 1.67 KB
/
437_Path_Sum_III.py
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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# https://leetcode.com/problems/path-sum-iii/discuss/91892/Python-solution-with-detailed-explanation
class Solution(object):
# def find_paths(self, root, target):
# if root:
# return int(root.val == target)
# + self.find_paths(root.left, target - root.val)
# + self.find_paths(root.right, target - root.val)
# return 0
# def pathSum(self, root, sum):
# """
# :type root: TreeNode
# :type sum: int
# :rtype: int
# """
# if root:
# return self.find_paths(root, sum)
# + self.pathSum(root.left, sum)
# + self.pathSum(root.right, sum)
# return 0
def pathSumHelper(self, root, target, so_far, cache):
if root:
# complement == 1, root->curr path
complement = so_far + root.val - target
if complement in cache:
# S->E path, sum(root->S)-sum(root->E) = target
self.result += cache[complement]
cache[so_far + root.val] = cache.get(so_far + root.val, 0) + 1
self.pathSumHelper(root.left, target, so_far + root.val, cache)
self.pathSumHelper(root.right, target, so_far + root.val, cache)
cache[so_far + root.val] -= 1
return
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
self.result = 0
self.pathSumHelper(root, sum, 0, {0: 1})
return self.result