LeetCode 428. Serialize and Deserialize N-ary Tree
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-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 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:
Nis 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.
题解:
Serialize the tree with preorder traverse. For each node, serialize its value, then the size of its children.
Then example tree is serialized like 1,3, 3,2, 5,0, 6,0, 2,0, 4,0
When deserializing, convert the string into queue. Poll the first 2 numbers, first is the value, second is the size of children.
Construct the new Node, and for the size of its children, recursize call and add result back to its children.
Time Complexity: serialize, O(n). deserialize, O(n).
Space: O(n).
AC Java:
/*
// 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> ls = new LinkedList<String>();
serial(root, ls); return String.join(",", ls);
} private void serial(Node root, List<String> ls){
if(root == null){
return;
} ls.add(String.valueOf(root.val));
ls.add(String.valueOf(root.children.size()));
for(Node child : root.children){
serial(child, ls);
}
} // Decodes your encoded data to tree.
public Node deserialize(String data) {
if(data == null || data.length() == 0){
return null;
} LinkedList<String> que = new LinkedList<String>();
que.addAll(Arrays.asList(data.split(",")));
return deserial(que);
} private Node deserial(LinkedList<String> que){
int val = Integer.valueOf(que.poll());
int size = Integer.valueOf(que.poll()); Node root = new Node(val, new ArrayList<Node>());
for(int i = 0; i<size; i++){
root.children.add(deserial(que));
} return root;
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
类似Serialize and Deserialize Binary Tree, Serialize and Deserialize BST.
LeetCode 428. Serialize and Deserialize N-ary Tree的更多相关文章
- [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 tha ...
- [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 ...
- [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
- [leetcode]449. Serialize and Deserialize BST设计BST的编解码
这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...
随机推荐
- C/C++ 快速排序实现
#include<iostream> using namespace std; void qS(int *array,int left,int right){ if(left<rig ...
- 通过fsockopen()方法从中国福彩网获取双色球历史中奖数据
# 以下代码基于 CI 框架 # public function history_draw($page = 0) { set_time_limit(0); $page++; $up2now = dat ...
- day15——有参装饰器、多个装饰器装饰一个函数
day15 装饰器的进阶 有参装饰器 @auth(chose) 相等于以下两行代码的解构 wrapper = auth(chose) foo = wrapper(foo) # dic = {'user ...
- Python之路【第十一篇】:Python面向对象之封装
一 引子 从封装本身的意思去理解,封装就好像是拿来一个麻袋,把青菜,土豆,花菜,还有苹果一起装进麻袋,然后把麻袋封上口子.照这种逻辑看,封装=‘隐藏’,这种理解是相当片面的. 在面向对象中这个麻袋就是 ...
- Redis缓存雪崩、击穿、穿透
参考大佬 前言 Redis在互联网技术存储方面使用如此广泛,几乎所有的后端技术面试官都要在Redis的使用和原理方面对小伙伴们进行360°的刁难.作为一个在互联网公司面一次拿一次offer的面霸(请允 ...
- maven安装本地jar到本地仓库
注册到本地仓库 mvn install:install-file -DgroupId=cn.endv -DartifactId=endv-api -Dversion=1.0.1 -Dpackaging ...
- CentOS -- 新建用户并使能密钥登录
目录 1. 新建用户 2. 为新用户授权 2.1. 方法一:把新用户添加到wheel用户组中 2.2. 方法二:把新用户添加到sudoers列表中 3. 新用户使能 SSH 密钥登录 4. 其它 4. ...
- OO第三单元作业总结
OO第三单元作业总结--JML 第三单元的主题是JML规格的学习,其中的三次作业也是围绕JML规格的实现所展开的(虽然感觉作业中最难的还是如何正确适用数据结构以及如何正确地对于时间复杂度进行优化). ...
- mongos
官方文档:https://docs.mongodb.com/manual/reference/program/mongos/#bin.mongos mongos是MongoDB shard的缩写,它是 ...
- linux 中常遇到的问题
1.上传文件是速度为零 xshell连接对应的Ubuntu服务器上在Ubuntu服务器上安装lrzszsudo apt install lrzsz xshell连接对应的centos服务器上 yum ...