Simple Readable Python Solution


  • 0
    M
        def tree2str(self, t):
            """
            :type t: TreeNode
            :rtype: str
            """
            if not t:
                return ''
            s = str(t.val)
            if t.left or t.right:
                if t.left:
                    s += '(%s)' % self.tree2str(t.left)
                else:
                    s += '()'
                if t.right:
                    s += '(%s)' % self.tree2str(t.right)
            return s
    

Log in to reply
 

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