Python Simple Solution

  • 4
    class Solution(object):
        def tree2str(self, t):
            :type t: TreeNode
            :rtype: str
            if not t:
                return ""
            res = ""
            left = self.tree2str(t.left)
            right = self.tree2str(t.right)
            if left or right:
                res += "(%s)" % left
            if right:
                res += "(%s)" % right
            return str(t.val) + res

  • 0

    Why did you convert left and right to str when it's already in str?

  • 0

    @livelearn Oh. That's my mistake. Fixed!

Log in to reply

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