python - path sum II

  • 0
    # 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 npath(self, root, target, prefix, answer):
            if root:
                prefix += [root.val]
                if not (root.left or root.right):
                    if sum(prefix) == target:
                    self.npath(root.left, target, prefix, answer)
                    self.npath(root.right, target, prefix, answer)
            return answer
        def pathSum(self, root, sum):
            :type root: TreeNode
            :type sum: int
            :rtype: List[List[int]]
            return self.npath(root, sum, [], [])

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.