Python 5 lines solution

  • 0
    def pathSum(self, root, exp):
        if not root:
            return []
        if not root.left and not root.right:
            return [[root.val]] if root.val == exp else []
        return [[root.val] + i for i in self.pathSum(root.left, exp-root.val)] + [[root.val] + i for i in self.pathSum(root.right, exp-root.val)] 

Log in to reply

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