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:
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.
题解:
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,先序遍历确实是可以 唯一得确定. ...
随机推荐
- Vuecli3
第一步安装 npm install -g @vue/cli 第二步关于项目配置 因为cli3去除了cli2中index.html 转而存到了publi文件中 如果需要配置跨域 页面入口 打包文件路径都 ...
- 一文快速入门Docker
Docker提供一种安全.可重复的环境中自动部署软件的方式,拉开了基于与计算平台发展方式的变革序幕.如今Docker在互联网公司使用已经非常普遍.本文用十分钟时间,带你快速入门Docker. Dock ...
- python 字符串替换功能 string.replace()可以用正则表达式,更优雅
说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...
- linux配置环境jdk
条件:将jdk安装好,如果没有安装请看这里:linux(Centos7系统)中安装JDK.Tomcat.Mysql 步骤如下: linux中,环境变量是在 /etc/profile 中修改文件 vi ...
- C# vb .net实现圆角矩形特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的圆角矩形效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...
- 2019 用友网络java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条. 用友网络等公司offer,岗位是Java后端开发,最终选择去了 用友网络. 面试了很多家公司,感觉大部分公司考察 ...
- 一个非常有趣的爬虫小练习带ocr识别的
有个小的想法,想找一找 形近字 .百度一搜索,百度文库有一个,收费4元.而且我觉得字数不是太多.想自己弄一个,于是找到了 这个网站 http://www.fantiz5.com/xingjinzi/ ...
- pandas-09 pd.groupby()的用法
pandas-09 pd.groupby()的用法 在pandas中的groupby和在sql语句中的groupby有异曲同工之妙,不过也难怪,毕竟关系数据库中的存放数据的结构也是一张大表罢了,与da ...
- 激活windows去掉右下角水印
激活windows去掉右下角水印 //需要隔一段时间执行一次 // 卸载已有的激活产品slmgr.vbs /upk // 重新按照激活产品slmgr /ipk NPPR9-FWDCX-D2C8J-H ...
- MES选型很困惑?避开这三个禁忌!
MES系统的选型除了要充分剖析自己企业,掌握自己企业的需要.信息化的目标.自身的特点外,还要完全了解MES系统供应商,对其实力.软件性能.服务.用户.软件实施速度.价格进行了解与分析,这也是MES系统 ...