Python Solution Easy to Understand


  • 0
    A
    def tree2str(self, t):
            """
            :type t: TreeNode
            :rtype: str
            """
            if t == None:
                return ''
            if t.left == None and t.right == None:
                return str(t.val)
            if t.right == None:
                return str(t.val) + '(' + self.tree2str(t.left) +')'
    
            else:
                return str(t.val) + '(' + self.tree2str(t.left) +')' + '(' + self.tree2str(t.right) + ')'
    

Log in to reply
 

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