Skip to main content

606 - Construct String from Binary Tree

Details

KeyValue
Linkhttps://leetcode.com/problems/construct-string-from-binary-tree/
LanguagePython 3
Runtime65 ms, faster than 77.11% of Python3 online submissions for Construct String from Binary Tree
Memory Usage16.4 MB, less than 72.23% of Python3 online submissions for Construct String from Binary Tree
DatastructuresTreeNode
AlgorithmsDFS w/ Recursion
ComplexityTime: O(N) Memory: O(N)

Procedure

  1. ...

Code

Option 1: Faster

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if not root: return ""

result = str(root.val)
if root.left:
result += f'({self.tree2str(root.left)})'
if root.right:
result += f'({self.tree2str(root.right)})'
elif root.right:
result += f'()({self.tree2str(root.right)})'

return result

Option 2: Terse

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def tree2str(self, root: Optional[TreeNode]) -> str:
if root is None: return ''
if root.left == None and root.right == None: return str(root.val)
if root.right == None: return f'{str(root.val)}({self.tree2str(root.left)})'
return f'{str(root.val)}({self.tree2str(root.left)})({self.tree2str(root.right)})'