88%-rank solution in python


  • 0
    E
    class Solution(object):
        def pathSum(self, root, sum):
            """
            :type root: TreeNode
            :type sum: int
            :rtype: List[List[int]]
            """
            res = []
    
            def traverse(node, path, __sum):
                if node is None or node.val is None:
                    return
                path.append(node.val)
                if node.val == __sum and node.left == node.right == None:
                    res.append(path)
                    return
                if node.left:
                    traverse(node.left, path[:], __sum - node.val)
                if node.right:
                    traverse(node.right, path[:], __sum - node.val)
    
            traverse(root, [], sum)
            return res
    

Log in to reply
 

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