Recursive Python Solution

  • 0
    def binaryTreePaths(self, root):
            if not root:
                return []
            if not root.left and not root.right:
                return [str(root.val)]
            left_paths = self.binaryTreePaths(root.left)
            right_paths = self.binaryTreePaths(root.right)
            all_paths = left_paths + right_paths
            return ['%s->%s' % (root.val, path) for path in all_paths]

Log in to reply

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