[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一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
随机推荐
- Servlet基本_画面遷移
画面遷移方法は.下記ようがある.・リクエストのディスパッチ・リダイレクト(画面から) 1.ディスパッチ1)概念サーブレットから他のリソース(サーブレット.JSP.Htmlなど)にリクエストを転送するこ ...
- 调用DATASNAP+FIREDAC的远程方法有时会执行二次SQL或存储过程的BUG(转永喃兄)
调用DATASNAP+FIREDAC的远程方法有时会执行二次SQL或存储过程的BUG 1)查询会重复执行的情形:Result := DATASETPROVIDER.Data会触发它关联的DATASET ...
- 一次docker中的nginx进程响应慢问题定位记录
有个ft测试的环境,其中nginx使用docker发布的.测试用例是curl的时候,没有获得nginx的响应. docker ps CONTAINER ID IMAGE COMMAND CREATED ...
- 找某個ColumnName在那些Tables
想找ColumnName叫CRE_USR的欄位在那些Table呢? (For SQL Server) SELECT o.name, o.* FROM syscolumns c INNER JOIN s ...
- [PHP]基于角色的访问控制RBAC
---------------------------------------------------------------------------------------------------- ...
- 新书预告 ArcGIS跨平台开发系列第一本
新书预告 ArcGIS跨平台开发系列第一本 候选题目: ArcGIS Runtime开发实验实习教程 ArcGIS Runtime开发案例教程 简介: GIS最新现代开发理念打造的跨所有移动和桌面平台 ...
- 配置 SQL Server 2008 Email 发送以及 Job 的 Notification通知功能
SQL Server 2008配置邮件的过程就不写了,网上的案例太多了. http://www.cnblogs.com/woodytu/p/5154526.html 这个案例就不错. 主要写下配置完后 ...
- EF 控制code-first生成的数据库表名的单复数
原地址:https://blog.csdn.net/winnyrain/article/details/51248410 在Code-First中,默认生成的数据库表的名称为类型的复数形式,如Mode ...
- 吴裕雄 python oracle操作数据库(4)
import cx_Oracle conn = cx_Oracle.connect("scott/admin@localhost:1521/orcl")cursor = conn. ...
- 删除kafka topic
1.因为项目原因,kakfa通道中经常造成数据阻塞,导致kafka通道中数据量过大,因此我需要将kakfa通道中数据清除(个人项目原因,一直使用一个消费者,只要保证当前消费者不在消费之前很久的数据就可 ...