[leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树
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 an N-ary tree. An N-ary tree is a rooted tree in which each node has no more than N children. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that an N-ary 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 3-ary
tree
as [1 [3[5 6] 2 4]]
. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note:
N
is in the range of[1, 1000]
- Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
思路
1. preorder recursive traversal
2. add number of children after root val, in order to know when to terminate
1 3 3 2 5 0 6 0 2 0 4 0
代码
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children; public Node() {} public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Codec {
// Encodes a tree to a single string.
public String serialize(Node root) {
List<String> list = new LinkedList<>();
buildString(root, list);
return String.join(",", list);
} private void buildString(Node root, List<String> list) {
if (root == null) return; list.add(String.valueOf(root.val));
list.add(String.valueOf(root.children.size()));
for (Node child : root.children) {
buildString(child, list);
} } // Decodes your encoded data to tree.
public Node deserialize(String data) {
if (data.length() == 0) return null;
String[] strArr = data.split(",");
Queue<String> queue = new LinkedList<>();
Collections.addAll(queue, strArr);
return buildTree(queue);
} private Node buildTree(Queue<String> queue) {
// match the given constructor form
Node root = new Node();
root.val = Integer.parseInt(queue.poll());
int size = Integer.parseInt(queue.poll());
root.children = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
root.children.add(buildTree(queue));
}
return root;
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
[leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树的更多相关文章
- LeetCode 428. Serialize and Deserialize N-ary Tree
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ 题目: Serialization is the ...
- [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 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 二叉树的序列化和反序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树
原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...
- [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
随机推荐
- jquery接触初级----- 一种新奇的选择器用法
今天看到一个新奇的jquery 选择器的用法,因为以前没有见过,所以记录下来 1.jquery 选择器: 给body添加一个元素,添加元素的时候,同时把属性和点击事件都一起进行添加 <!DOCT ...
- DO and DOES Reduction
DO and DOES Reduction Share Tweet Share Tagged With: DO and DOES Reductions ‘Do’ and ‘does’ can be r ...
- 利用jQuery扩展接口为jQuery框架定义了两个自定义函数,然后调用这两个函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Hibernate学习笔记3.2(Hibernate组建映射)
1.组建映射 可以存在一个表里面 Husband.java package com.bjsxt.hibernate; import javax.persistence.Embedded; import ...
- 计算平面面积和斜面面积-ArcGIS案例学习笔记
计算平面面积和斜面面积-ArcGIS案例学习笔记 联系方式:谢老师,135_4855_4328,xiexiaokui#139.com 数据:实验数据\Chp8\Ex5\demTif.tif 平面面积= ...
- shiro 密码的MD5盐值加密
- SQL Server 生成 数据字典 / 数据库文档
1. 工具生成 2.SQL语句生成 参考地址:http://blog.csdn.net/qq289523052/article/details/22174721 1.在 表 上右键 - 扩展属性 - ...
- pandas.read_csv用法(转)
的数据结构DataFrame,几乎可以对数据进行任何你想要的操作. 由于现实世界中数据源的格式非常多,pandas也支持了不同数据格式的导入方法,本文介绍pandas如何从csv文件中导入数据. 从上 ...
- 对于“2017面向对象程序设计(Java)第五周工作总结”存在问题的反馈及本周教学计划
一:问题反馈 “上周我们学习的新内容主要是第五章,并对第四章内容做了巩固.从学生上交的实验报告完成情况以及学习Java心得博客中的反馈可以看出,学生对构造器.重载.超类.多态.抽象类这几个概念理解的不 ...
- pipy国内镜像的网址
pipy国内镜像目前有:豆瓣 http://pypi.douban.com/simple/阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https: ...