[LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化
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.
Example:
You may serialize the following tree: 1
/ \
2 3
/ \
4 5 as"[1,2,3,null,null,4,5]"
Clarification: The above format is the same as how LeetCode 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.
序列化是是指将数据结构或物件状态转换成可取用格式(例如存成档案,存于缓冲,或经由网络中传送),以留待后续在相同或另一台计算机环境中,能恢复原先状态的过程,还原的过程叫做反序列化。比如例子中的把树序列化成数组,还可以由这个数组在反序列化变回树。
根据题目序列化的形式,可以先序遍历的递归或者层序遍历的非递归:
需要接入输入和输出字符串流istringstream和ostringstream,对于序列化,从根节点开始,如果节点存在,则将值存入输出字符串流,然后分别对其左右子节点递归调用序列化函数即可。对于反序列化,先读入第一个字符生成一个根节点,然后再对根节点的左右子节点递归调用去序列化函数即可。
Java:level order traversal
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root==null){
return "";
} StringBuilder sb = new StringBuilder(); LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root);
while(!queue.isEmpty()){
TreeNode t = queue.poll();
if(t!=null){
sb.append(String.valueOf(t.val) + ",");
queue.add(t.left);
queue.add(t.right);
}else{
sb.append("#,");
}
} sb.deleteCharAt(sb.length()-1);
System.out.println(sb.toString());
return sb.toString();
} // Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data==null || data.length()==0)
return null; String[] arr = data.split(",");
TreeNode root = new TreeNode(Integer.parseInt(arr[0])); LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root); int i=1;
while(!queue.isEmpty()){
TreeNode t = queue.poll(); if(t==null)
continue; if(!arr[i].equals("#")){
t.left = new TreeNode(Integer.parseInt(arr[i]));
queue.offer(t.left); }else{
t.left = null;
queue.offer(null);
}
i++; if(!arr[i].equals("#")){
t.right = new TreeNode(Integer.parseInt(arr[i]));
queue.offer(t.right); }else{
t.right = null;
queue.offer(null);
}
i++;
} return root;
}
Java: Preorder traversal
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root==null)
return null; Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
StringBuilder sb = new StringBuilder(); while(!stack.isEmpty()){
TreeNode h = stack.pop();
if(h!=null){
sb.append(h.val+",");
stack.push(h.right);
stack.push(h.left);
}else{
sb.append("#,");
}
} return sb.toString().substring(0, sb.length()-1);
} // Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == null)
return null; int[] t = {0};
String[] arr = data.split(","); return helper(arr, t);
} public TreeNode helper(String[] arr, int[] t){
if(arr[t[0]].equals("#")){
return null;
} TreeNode root = new TreeNode(Integer.parseInt(arr[t[0]])); t[0]=t[0]+1;
root.left = helper(arr, t);
t[0]=t[0]+1;
root.right = helper(arr, t); return root;
}
Java:
public class Codec {
private static final String spliter = ",";
private static final String NN = "X"; // Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
buildString(root, sb);
return sb.toString();
} private void buildString(TreeNode node, StringBuilder sb) {
if (node == null) {
sb.append(NN).append(spliter);
} else {
sb.append(node.val).append(spliter);
buildString(node.left, sb);
buildString(node.right,sb);
}
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
Deque<String> nodes = new LinkedList<>();
nodes.addAll(Arrays.asList(data.split(spliter)));
return buildTree(nodes);
} private TreeNode buildTree(Deque<String> nodes) {
String val = nodes.remove();
if (val.equals(NN)) return null;
else {
TreeNode node = new TreeNode(Integer.valueOf(val));
node.left = buildTree(nodes);
node.right = buildTree(nodes);
return node;
}
}
}
Python:
class Codec: def serialize(self, root):
def serializeHelper(node):
if not node:
vals.append('#')
else:
vals.append(str(node.val))
serializeHelper(node.left)
serializeHelper(node.right)
vals = []
serializeHelper(root)
return ' '.join(vals) def deserialize(self, data):
def deserializeHelper():
val = next(vals)
if val == '#':
return None
else:
node = TreeNode(int(val))
node.left = deserializeHelper()
node.right = deserializeHelper()
return node
def isplit(source, sep):
sepsize = len(sep)
start = 0
while True:
idx = source.find(sep, start)
if idx == -1:
yield source[start:]
return
yield source[start:idx]
start = idx + sepsize
vals = iter(isplit(data, ' '))
return deserializeHelper()
Python:
class Codec: def serialize(self, root):
def doit(node):
if node:
vals.append(str(node.val))
doit(node.left)
doit(node.right)
else:
vals.append('#')
vals = []
doit(root)
return ' '.join(vals) def deserialize(self, data):
def doit():
val = next(vals)
if val == '#':
return None
node = TreeNode(int(val))
node.left = doit()
node.right = doit()
return node
vals = iter(data.split())
return doit()
C++:
class Codec {
public:
string serialize(TreeNode* root) {
ostringstream out;
serialize(root, out);
return out.str();
} TreeNode* deserialize(string data) {
istringstream in(data);
return deserialize(in);
}
private:
void serialize(TreeNode* root, ostringstream& out) {
if (!root) {
out << "# ";
return;
}
out << root->val << " ";
serialize(root->left, out);
serialize(root->right, out);
} TreeNode* deserialize(istringstream& in) {
string val;
in >> val;
if (val == "#") return nullptr;
TreeNode* root = new TreeNode(stoi(val));
root->left = deserialize(in);
root->right = deserialize(in);
return root;
}
};
C++:
class Codec {
public: string serialize(TreeNode* root) {
ostringstream out;
serialize(root, out);
return out.str();
} TreeNode* deserialize(string data) {
istringstream in(data);
return deserialize(in);
}
private:
enum STATUS {
ROOT_NULL = 0x0,
ROOT = 0x1,
LEFT = 0x2,
RIGHT = 0x4
}; void serialize(TreeNode* root, ostringstream& out) {
char status = 0;
if (root) status |= ROOT;
if (root && root->left) status |= LEFT;
if (root && root->right) status |= RIGHT;
out.write(&status, sizeof(char));
if (!root) return;
out.write(reinterpret_cast<char*>(&(root->val)), sizeof(root->val));
if (root->left) serialize(root->left, out);
if (root->right) serialize(root->right, out);
} TreeNode* deserialize(istringstream& in) {
char status;
in.read(&status, sizeof(char));
if (!status & ROOT) return nullptr;
auto root = new TreeNode(0);
in.read(reinterpret_cast<char*>(&root->val), sizeof(root->val));
root->left = (status & LEFT) ? deserialize(in) : nullptr;
root->right = (status & RIGHT) ? deserialize(in) : nullptr;
return root;
}
};
Followup: 如果是 n-ary怎么办?
All LeetCode Questions List 题目汇总
[LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化的更多相关文章
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
- 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)
[抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...
- [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- Leetcode 297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...
- [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
随机推荐
- Unity进阶之ET网络游戏开发框架 07-修正游客登录的异步BUG
版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...
- BZOJ1485: [HNOI2009]有趣的数列(卡特兰数+快速幂)
题目链接 传送门 题面 思路 打表可以发现前六项分别为1,2,5,12,42,132,加上\(n=0\)时的1构成了卡特兰数的前几项. 看别人的题解说把每一个数扫一遍,奇数项当成入栈,偶数项当成出栈, ...
- win10下无法安装loadrunner,提示“管理员已阻止你运行此应用”
如下图: 1.再次进入控制面板,并且选择用户账户后把最下面的[更改用户账户控制设置],里面有个滑条,把滑条拉到最下面的[从不通知]上面并且确定. 2.按[Win+R]快捷键打开运行,输入 gpedit ...
- selenium常用的API(七)判断元素是否可见
web页面不可见的元素虽不在页面上显示,但是存在于DOM树中,这些元素webdriver也能找到. element.is_displayed()方法可以判断元素是否在页面上显示,如果显示返回True, ...
- CodeForces - 115E:Linear Kingdom Races (DP+线段树+lazy)
pro: 从左到有有N个车道,都有一定程度损坏,所以有不同的修理费a[]: 有M场比赛,每场比赛的场地是[Li,Ri],即如果这个区间的车道都被修理好,则可以举办这个比赛,并且收益是Pi.问最多得到多 ...
- 题解 LA2889
题目大意 多组数据,每组数据给出一个正整数 \(n\),输出第 \(n\) 大的回文数(即 \(1,2,3,\cdots\)). 分析 不难发现,\(n\) 位的回文数有 \(9*10^{\lfloo ...
- 15-Flutter移动电商实战-商品推荐区域制作
1.推荐商品类的编写 这个类接收一个List参数,就是推荐商品的列表,这个列表是可以左右滚动的. /*商品推荐*/class Recommend extends StatelessWidget { ...
- 百度OCR文字识别-Android安全校验
本文转载自好基友upuptop:https://blog.csdn.net/pyfysf/article/details/86438769 效果图: 如下为文章正文: 百度OCR接口使用总结:之前总结 ...
- [HTML5] Using HTMLPortalElement to improve MPA preformance
For multi pages application, it is very slow to navgiate between page by page, because it needs to r ...
- SpringBoot第二节(SpringBoot整合Mybatis)
1.创建Spring Initiallizr项目 一直点击下一步 2.引入依赖 <dependencies> <dependency> <groupId>org.s ...