Path Sum II in Python

  • 0


      class Solution(object):
         def pathSum(self, root, sum):
             res = []
             if root == None:
                 return res
              self.dfs(root, 0, [root.val], res, sum)
              return res
    def dfs(self, root, currsum, valist, res, sum):
        if root.left == None and root.right == None:
            if currsum == sum-root.val:
            return res
        if root.left:
            self.dfs(root.left, currsum+root.val, valist+[root.left.val], res, sum)
        if root.right:
            self.dfs(root.right, currsum+root.val, valist+[root.right.val], res, sum)


Log in to reply

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