[leetcode297]Serialize and Deserialize Binary Tree
来源:互联网 发布:军事题材的网络电影 编辑:程序博客网 时间:2024/05/21 01:52
问题描述:
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
For example, you may serialize the following tree
1 / \ 2 3 / \ 4 5as
"[1,2,3,null,null,4,5]"
, just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
Credits:Special thanks to @Louis1992 for adding this problem and creating all test cases.
算法:
代码:
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Nonefrom collections import dequeclass Codec(object): def serialize(self, root): """Encodes a tree to a single string. :type root: TreeNode :rtype: str """ container = [] que = deque([root]) while que: cur = que.popleft() if cur is None: container.append('null') continue else: container.append(str(cur.val)) que.append(cur.left) que.append(cur.right) return ",".join(container) def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ if not data: return None container = [TreeNode(int(ele)) if ele != 'null' else None for ele in data.split(',')] parent_idx = 0 sub_idx = 1 # root = None while sub_idx+1 < len(container) and parent_idx < len(container): parent = container[parent_idx] if parent is None: parent_idx += 1 continue else: parent.left = container[sub_idx] parent.right = container[sub_idx+1] parent_idx += 1 sub_idx += 2 return container[0]# Your Codec object will be instantiated and called as such:# codec = Codec()# codec.deserialize(codec.serialize(root))
- [leetcode297]Serialize and Deserialize Binary Tree
- leetcode297:二叉树的序列化和反序列化(Serialize and Deserialize Binary Tree)
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree & Serialize and Deserialize BST
- Serialize and deserialize a binary tree
- Serialize and Deserialize a binary tree
- 8205A
- UBIFS根文件系统制作及移植
- windows和linux下简单的软件级调整相机曝光时间(或其他参数)的方式
- LeetCode(147) Insertion Sort List
- Maven和Ant的区别
- [leetcode297]Serialize and Deserialize Binary Tree
- Java内部类的调用
- java面试:兔子问题
- hdoj 1269 迷宫城堡
- tnsnames.ora中UR=A配置使用
- 南阳理工 括号配对题解
- 【ACM】1020:迭代法
- 优化
- VisualVM 性能调优过程(测试demo)