I think this is a minimal and fast solution :
import re class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def serialize(root: Node) -> str: if root is None: return "" return f"({root.val},{serialize(root.left)},{serialize(root.right)})" def deserialize(serialized: str) -> Node: if serialized is None: return None match = re.search(r"\(([\w\.]+),(\(.*\))?,(\(.*\))?\)", serialized) if match: val, left, right = match.groups() return Node(val=val, left=deserialize(left), right=deserialize(right)) node = Node("root", Node("left", Node("left.left")), Node("right")) assert deserialize(serialize(node)).left.left.val == "left.left"
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think this is a minimal and fast solution :