# 序列化和反序列化一个二叉树

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 `# Definition for a binary tree node.` `# class TreeNode(object):` `#     def __init__(self, x):` `#         self.val = x` `#         self.left = None` `#         self.right = None` `class` `Codec:` `    ``def` `serialize(``self``, root):` `        ``"""Encodes a tree to a single string.` `        `  `        ``:type root: TreeNode` `        ``:rtype: str` `        ``"""` `        ``if` `root !``=` `None``:` `            ``checkList ``=` `[root]` `        ``else``:` `            ``checkList ``=` `[]` `        ``AllNodeList ``=` `[]` `        ``while` `checkList !``=` `[]:` `            ``nextList ``=` `[]` `            ``for` `Node ``in` `checkList:` `                ``if` `Node !``=` `‘#‘``:` `                    ``AllNodeList.append(``str``(Node.val))` `                    ``if` `Node.left ``=``=` `None``:` `                        ``nextList.append(``‘#‘``)` `                    ``else``:` `                        ``nextList.append(Node.left)` `                    ``if` `Node.right ``=``=` `None``:` `                        ``nextList.append(``‘#‘``)` `                    ``else``:` `                        ``nextList.append(Node.right)` `                ``else``:` `                    ``AllNodeList.append(Node)` `            ``if` `len``(``set``(nextList)) ``=``=` `1` `and` `‘#‘` `in` `nextList:` `                ``nextList ``=` `[]` `            ``checkList ``=` `nextList` `        ``return` `‘,‘``.join(AllNodeList)` `        `  `    ``def` `deserialize(``self``, data):` `        ``"""Decodes your encoded data to tree.` `        `  `        ``:type data: str` `        ``:rtype: TreeNode` `        ``"""` `        ``if` `data ``=``=` `‘‘:` `            ``currentLevel ``=` `[]` `            ``root ``=` `None` `        ``else``:` `            ``AllNodeList ``=` `data.split(``","``)` `            ``root ``=` `TreeNode(``int``(AllNodeList.pop(``0``)))` `            ``currentLevel ``=``[root]` `        ``while` `currentLevel !``=` `[] ``and` `AllNodeList!``=` `[]:` `            ``nextLevel ``=` `[]` `            ``for` `node ``in` `currentLevel:` `                ``leftValue ``=` `AllNodeList.pop(``0``)` `                ``if` `leftValue !``=` `‘#‘``:` `                    ``node.left ``=` `TreeNode(``int``(leftValue))` `                    ``nextLevel.append(node.left)` `                    `  `                ``rightValue ``=` `AllNodeList.pop(``0``)` `                ``if` `rightValue !``=` `‘#‘``:` `                    ``node.right ``=` `TreeNode(``int``(rightValue))` `                    ``nextLevel.append(node.right)` `            ``print``([node.val ``for` `node ``in` `nextLevel])` `            ``currentLevel ``=` `nextLevel` `        ``return` `root  ` `# Your Codec object will be instantiated and called as such:` `# codec = Codec()` `# codec.deserialize(codec.serialize(root))`

(0)
(0)