-
Notifications
You must be signed in to change notification settings - Fork 43
/
path-sum-iii.py
213 lines (184 loc) · 5.64 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
437. Path Sum III
Medium
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).
Example 1:
Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
Output: 3
Explanation: The paths that sum to 8 are shown.
Example 2:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: 3
Constraints:
The number of nodes in the tree is in the range [0, 1000].
-109 <= Node.val <= 109
-1000 <= targetSum <= 1000
"""
# V0
# IDEA : BFS + DFS
# => USE BFS FIND EVERY NODE IN THE TREE, AND USE DFS GET THR PATH SUM ON EVERY NODE (FOUND BY BFS)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
res = [0]
que = collections.deque()
que.append(root)
while que:
node = que.popleft()
if not node:
continue
self.dfs(node, res, 0, sum)
que.append(node.left)
que.append(node.right)
return res[0]
def dfs(self, root, res, path, target):
if not root: return
path += root.val
if path == target:
res[0] += 1
self.dfs(root.left, res, path, target)
self.dfs(root.right, res, path, target)
# V1
# https://blog.csdn.net/xiaoxiaoley/article/details/79093996
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
def dfs(root,sum):
if root==None:
return 0
if root.val==sum:
return 1+dfs(root.left,0)+dfs(root.right,0)
return dfs(root.left,sum-root.val)+dfs(root.right,sum-root.val)
if root==None:
return 0
return dfs(root,sum)+self.pathSum(root.left,sum)+self.pathSum(root.right,sum)
# V1'
# https://www.jiuzhang.com/solution/path-sum-iii/#tag-highlight-lang-python
class Solution:
"""
@param root:
@param sum:
@return: nothing
"""
def DFS(self, root, su, tmp):
if None==root:
return 0
else:
flag=0
if su==tmp+root.val:
flag=1
return flag+self.DFS(root.left, su, tmp+root.val)+self.DFS(root.right, su, tmp+root.val)
def pathSum(self, root, su):
#write your code here
if None==root:
return 0
else:
return self.DFS(root, su, 0)+self.pathSum(root.left, su)+self.pathSum(root.right, su)
# V2
# https://blog.csdn.net/fuxuemingzhu/article/details/71097135
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
if not root: return 0
return self.dfs(root, sum) + self.pathSum(root.left, sum) + self.pathSum(root.right, sum)
def dfs(self, root, sum):
res = 0
if not root: return res
sum -= root.val
if sum == 0:
res += 1
res += self.dfs(root.left, sum)
res += self.dfs(root.right, sum)
return res
# V2' : BFS + DFS
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
res = [0]
que = collections.deque()
que.append(root)
while que:
node = que.popleft()
if not node:
continue
self.dfs(node, res, 0, sum)
que.append(node.left)
que.append(node.right)
return res[0]
def dfs(self, root, res, path, target):
if not root: return
path += root.val
if path == target:
res[0] += 1
self.dfs(root.left, res, path, target)
self.dfs(root.right, res, path, target)
# V3
# Time: O(n)
# Space: O(h)
import collections
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
def pathSumHelper(root, curr, sum, lookup):
if root is None:
return 0
curr += root.val
result = lookup[curr-sum] if curr-sum in lookup else 0
lookup[curr] += 1
result += pathSumHelper(root.left, curr, sum, lookup) + \
pathSumHelper(root.right, curr, sum, lookup)
lookup[curr] -= 1
if lookup[curr] == 0:
del lookup[curr]
return result
lookup = collections.defaultdict(int)
lookup[0] = 1
return pathSumHelper(root, 0, sum, lookup)
# Time: O(n^2)
# Space: O(h)
class Solution2(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
def pathSumHelper(root, prev, sum):
if root is None:
return 0
curr = prev + root.val
return int(curr == sum) + \
pathSumHelper(root.left, curr, sum) + \
pathSumHelper(root.right, curr, sum)
if root is None:
return 0
return pathSumHelper(root, 0, sum) + \
self.pathSum(root.left, sum) + \
self.pathSum(root.right, sum)