Skip to content

Instantly share code, notes, and snippets.

@fpaupier
Created June 12, 2021 10:17
Show Gist options
  • Save fpaupier/a3f00621502f53ac9782968cf0b709fc to your computer and use it in GitHub Desktop.
Save fpaupier/a3f00621502f53ac9782968cf0b709fc to your computer and use it in GitHub Desktop.
Serialization method for 297. Serialize and deserialize a binary tree
def serialize(self, root) -> str:
queue, serialize = [root], ""
while queue:
next_level = []
while queue:
node = queue.pop(0)
serialize += str(node.val) + "," if node else "null" + ","
if node: next_level.extend([node.left, node.right])
queue = next_level if any(next_level) else []
return serialize[:-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment