8 lines in python,48ms


  • 19
    W
    def binaryTreePaths(self, root):
        if not root:
            return []
        if not root.left and not root.right:
            return [str(root.val)]
        treepaths = [str(root.val)+'->'+path for path in self.binaryTreePaths(root.left)]
        treepaths += [str(root.val)+'->'+path for path in self.binaryTreePaths(root.right)]
        return treepaths

Log in to reply
 

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