原题链接在这里: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:

  1. N is in the range of [1, 1000]
  2. 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 TreeSerialize and Deserialize BST.

LeetCode 428. Serialize and Deserialize N-ary Tree的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode 297. Serialize and Deserialize Binary Tree

    https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...

  4. [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 ...

  5. LeetCode 449. Serialize and Deserialize BST

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...

  6. [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 ...

  7. [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码

    由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...

  9. [leetcode]449. Serialize and Deserialize BST设计BST的编解码

    这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...

随机推荐

  1. sublime text 疑难解决

    sublime text 白色边框方框解决方法 https://blog.csdn.net/weixin_43228019/article/details/82766316 Sublime Text提 ...

  2. Go 关键字Select

    select select 是Go语言中常用的一个关键字,Linux再也早也引入了这个函数,用来实现非阻塞的一种方式,一个select语句用来选择哪个case中的发送或接收操作可以被立即执行.它类似于 ...

  3. vue的特殊指令 v-if v-once v-bind v-for v-on v-model

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. IDEA 开发插件

    Alibaba Java Code Guidelines 阿里巴巴推出的一款Java代码规约扫描插件,按照<阿里巴巴Java开发手册>规定对代码风格以及质量进行实时检测.约束.强推.ecl ...

  5. SQL SERVER 数据库查询表信息

    SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号 = a.colorder, 字段名 = ...

  6. Stack实现

    栈的三种操作算法很简单 STACK-EMPTY(S) 1 if S.top == 0 2    return TRUE 3 else return FALSE PUSH(S, x) 1 S.top = ...

  7. FastJson前置属性过滤器

    FastJson前置属性过滤器 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p ...

  8. ArcGIS加载数据中常用的File文件方法总结

    在介绍ArcGIS中各种数据的打开方法时,我们用到了许多对于File文件的操作,在此做一个常用用法的总结.例如, 介绍ArcGIS中各种数据的打开方法——mxd(地图文档) 以方法一为例:运用Load ...

  9. 1.ASP.NET Core 中向 Razor Pages 应用添加模型

    右键单击“RazorPagesMovie”项目 >“添加” > “新建文件夹”. 将文件夹命名为“Models”.右键单击“Models”文件夹. 选择“添加” > “类”. 将类命 ...

  10. docker下安装mysql数据库

    因为用了.net core 所以想学习下使用docker: 项目中刚好要用到mysql数据库,所用用docker来安装一次,我使用的是5.6版本: 1.拉取官方镜像 docker pull mysql ...