Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Codec:
- def serialize(self, root):
- """Encodes a tree to a single string.
- :type root: TreeNode
- :rtype: str
- """
- this_level = [root]
- out = []
- if not root:
- return ','.join(out)
- while this_level:
- next_level = []
- for item in this_level:
- if item is not "#":
- if item.left: next_level.append(item.left)
- else: next_level.append("#")
- if item.right: next_level.append(item.right)
- else: next_level.append("#")
- out.append(str(item.val))
- else:
- out.append("#")
- this_level = next_level
- return ','.join(out)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement