原题链接在这里: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. 未安装发布所需的web发布扩展

    解决方案:需要安装web deploy 下载网站:https://www.iis.net/downloads/microsoft/web-deploy 假如还是打不开的话,估计时打开方式错误了, 要用 ...

  2. mysql 注意小结

    char 默认是1 个字符 char(12) 设置是12个字符 不管是中文还是英文或者数字只能有十二个 设置外键时,这时候外键对应的父键的字段要是主键 非空而且是唯一. create table t1 ...

  3. LeetCode二叉树Java模板

    public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } impor ...

  4. 1139 First Contact PAT (Advanced Level)

    原题链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 测试点分析: 首先来分析一下测试 ...

  5. pytest_assert断言

    前言 断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了.什么是断言呢? 简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试 failed ...

  6. arc079

    D. Decrease (Contestant ver.) 大意: 每次操作选一个最大数$-n$,其余数全$+1$. 要求构造一个序列$a$, 使得恰好$k$次操作后最大值不超过$n-1$. 只要让$ ...

  7. 处女篇:自用C#后端SqlHelper.cs类

    自用SqlHelper.cs类,此类来自软谋教育徐老师课程SqlHelper.cs! using System; using System.Collections; using System.Coll ...

  8. Bash速查表

    例 #!/usr/bin/env bash NAME="John" echo "Hello $NAME!" 变量 NAME="John" e ...

  9. 2、在NET中实现多线程

    1.System.Threading命名空间 System.Threading命名空间提供了使得可以多线程编程的类和接口 其中 (1)Thread类构成了C#多线程编程的支柱,他用于创建并控制线程 ( ...

  10. java之spring mvc之Controller配置的几种方式

    这篇主要讲解 controller配置的几种方式. 1. URL对应 Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 配置handlerMapping --> & ...