Clear Python solution


  • 0
    W
    def tree2str(self, t):
        """
        :type t: TreeNode
        :rtype: str
        """
        
        if not t:
            return ''
        elif not t.left and not t.right:
            return str(t.val)
        elif not t.left and t.right:
            return str(t.val) + "(" + ")" + "(" + self.tree2str(t.right) + ")"
        elif t.left and not t.right:
            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.