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
Python Simple Solution


@jedihy could you explain why "if left or right"? And also why str(t.val)? Thank you!