recursive and concise Python Solution


  • 1
    class Solution(object):
        def dfsPathSum(self, root, sum, com, res):
            if not root:
                return
            if sum - root.val == 0 and not root.left and not root.right:
                res.append(com + [root.val])
                return
            self.dfsPathSum(root.left, sum - root.val, com + [root.val], res)
            self.dfsPathSum(root.right, sum - root.val, com + [root.val], res)
        def pathSum(self, root, sum):
            """
            :type root: TreeNode
            :type sum: int
            :rtype: List[List[int]]
            """
            res = []
            self.dfsPathSum(root, sum, [], res)
            return res
    

Log in to reply
 

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