Simple Python Solution

  • 0

    Here is the simple python solution.

    def binaryTreePaths(self, root):
            result = []
            if not root or not root.val:
                return result
            if not root.left and not root.right: result.append(str(root.val))
            if root.left: result.extend(list((str(root.val) + "->" + l) for l in self.binaryTreePaths(root.left)))
            if root.right: result.extend(list((str(root.val) + "->" + r) for r in self.binaryTreePaths(root.right)))
            return result

Log in to reply

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