1
class Solution:
2
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
3
def util(node: TreeNode, sum_array) -> int:
4
t = [e - node.val for e in sum_array]
5
zeroes = t.count(0)
6
if node.left is None and node.right is None:
7
return zeroes
8
ansl, ansr = 0, 0
9
if node.left:
10
ansl = util(node.left, t + [targetSum])
11
if node.right:
12
ansr = util(node.right, t + [targetSum])
13
return ansl + ansr + zeroes
14

15
return util(root, [targetSum]) if root is not None else 0

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0