MCPcopy
hub / github.com/HuberTRoy/leetCode / pathSum

Method pathSum

Tree/PathSumII.py:38–66  ·  view source on GitHub ↗

:type root: TreeNode :type sum: int :rtype: List[List[int]]

(self, root, sum)

Source from the content-addressed store, hash-verified

36
37class Solution(object):
38 def pathSum(self, root, sum):
39 """
40 :type root: TreeNode
41 :type sum: int
42 :rtype: List[List[int]]
43 """
44 if not root:
45 return []
46
47 result = []
48
49 def helper(prev, root, sum, path):
50 if prev + root.val == sum:
51 if not root.left and not root.right:
52 result.append(list(map(int, path.split(' ')[1:]))+[root.val])
53 return True
54
55 if root.left:
56 helper(prev + root.val, root.left, sum, path=path+" "+str(root.val))
57 # return True
58
59 if root.right:
60 helper(prev + root.val, root.right, sum, path=path+" "+str(root.val))
61
62 return False
63
64 helper(0, root, sum, "")
65
66 return result

Callers

nothing calls this directly

Calls 1

helperFunction · 0.50

Tested by

no test coverage detected