python preorder solution


  • 0
    D

    proorder the tree

    def tree2str(self, t):
        """
        :type t: TreeNode
        :rtype: str
        """
        s=''
        if not t:
            return ''
        s+=str(t.val)
        if t.left or t.right:
            s+='('+self.tree2str(t.left)+')'
        if t.right:
            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.