Easy
Construct String from Binary Tree — Python
Full explanation · Time O(n) · Space O(h)
# Time: O(n)
# Space: O(h)
class Solution(object):
def tree2str(self, t):
"""
:type t: TreeNode
:rtype: str
"""
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