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]]. Note that this is just an example, you do not necessarily need to follow this format.

Or you can follow LeetCode's level order traversal serialization format, where each group of children is separated by the null value.

For example, the above tree may be serialized as [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14].

You do not necessarily need to follow the above suggested formats, there are many more different formats that work so please be creative and come up with different approaches yourself.

Constraints:

  • The height of the n-ary tree is less than or equal to 1000
  • The total number of nodes is between [0, 10^4]
  • Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
 

这道题让我们对N叉树进行序列化和去序列化,序列化就是将一个数据结构或物体转化为一个位序列,可以存进一个文件或者内存缓冲器中,然后通过网络连接在相同的或者另一个电脑环境中被还原,还原的过程叫做去序列化。现在让我们来序列化和去序列化一个二叉树,并给了我们例子。由于我们有了之前那道Serialize and Deserialize Binary Tree对二叉树的序列化和去序列化的基础,那么这道N叉树的方法也是大同小异了。首先使用先序遍历的递归解法,递归的写法就十分的简洁了,对于序列化,我们需要一个helper函数,里面首先判断结点,若为空,则结果res加上一个井字符,否则加上当前结点值,跟一个空格,再加上子结点的个数值,再跟一个空格。之后就是遍历子结点了,对每个子结点都调用递归函数即可。去序列函数需要用一个字符串流类来帮助读字符,这个类是按空格来断开字符串的,所以我们在序列化的时候中间都是用的空格。我们同样需要一个helper函数,首先读出结点值,如果读出了井字号,直接返回空。否则继续读出子结点的个数,有了结点值我们就可以新建一个结点了,同时知道了子结点的个数,那么我们就循环调用递归函数相同的次数,将返回的子结点加入子结点数组即可,参见代码如下:

解法一:

class Codec {
public: // Encodes a tree to a single string.
string serialize(Node* root) {
string res;
serializeHelper(root, res);
return res;
} void serializeHelper(Node* node, string& res) {
if (!node) res += "#";
else {
res += to_string(node->val) + " " + to_string(node->children.size()) + " ";
for (auto child : node->children) {
serializeHelper(child, res);
}
}
} // Decodes your encoded data to tree.
Node* deserialize(string data) {
istringstream iss(data);
return deserializeHelper(iss);
} Node* deserializeHelper(istringstream& iss) {
string val = "", size = "";
iss >> val;
if (val == "#") return NULL;
iss >> size;
Node *node = new Node(stoi(val), {});
for (int i = ; i < stoi(size); ++i) {
node->children.push_back(deserializeHelper(iss));
}
return node;
}
};

我们还可以使用层序遍历的迭代写法,序列化的函数相对来说好一点,还是先判空,若为空,直接返回井字号。否则就使用队列,加入根结点,然后就进行while循环,先取出队首结点,然后res加入结点值,再加入空格,加入子结点个数,再加上空格。之后再把每一个子结点都加入队列中即可。去序列化函数稍稍复杂一些,还是要用字符流类来读取字符,需要用两个队列,分别来保存结点,和子结点个数。首先我们先取出结点值,如果是井字号,直接返回空。否则再取出子结点个数,我们先根据之前取出的结点值新建一个结点,然后加入结点队列,把子结点个数加入个数队列。然后就开始遍历了,首先分别取出结点队列和个数队列的队首元素,然后循环子结点个数次,再取出结点值,和子结点个数,如果其中某个值没取出来,就break掉。根据取出的结点值新建一个结点,然后将结点值加入结点队列,子结点个数加入个数队列,然后将子结点加入子结点数组,参见代码如下:

解法二:

class Codec {
public: // Encodes a tree to a single string.
string serialize(Node* root) {
if (!root) return "#";
string res;
queue<Node*> q{{root}};
while (!q.empty()) {
Node *t = q.front(); q.pop();
res += to_string(t->val) + " " + to_string(t->children.size()) + " ";
for (Node *child : t->children) {
q.push(child);
}
}
return res;
} // Decodes your encoded data to tree.
Node* deserialize(string data) {
istringstream iss(data);
queue<Node*> qNode;
queue<int> qSize;
string val = "", size = "";
iss >> val;
if (val == "#") return NULL;
iss >> size;
Node *res = new Node(stoi(val), {}), *cur = res;
qNode.push(cur);
qSize.push(stoi(size));
while (!qNode.empty()) {
Node *t = qNode.front(); qNode.pop();
int len = qSize.front(); qSize.pop();
for (int i = ; i < len; ++i) {
if (!(iss >> val)) break;
if (!(iss >> size)) break;
cur = new Node(stoi(val), {});
qNode.push(cur);
qSize.push(stoi(size));
t->children.push_back(cur);
}
}
return res;
}
};

类似题目:

Serialize and Deserialize BST

Serialize and Deserialize Binary Tree

Encode N-ary Tree to Binary Tree

参考资料:

https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/

https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Serialize and Deserialize N-ary Tree N叉搜索树的序列化和去序列化的更多相关文章

  1. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

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

  2. [LeetCode] Serialize and Deserialize Binary Tree

    Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...

  3. LeetCode——Serialize and Deserialize Binary Tree

    Description: Serialization is the process of converting a data structure or object into a sequence o ...

  4. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

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

  5. LeetCode初级算法--树02:验证二叉搜索树

    LeetCode初级算法--树02:验证二叉搜索树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  6. Leetcode: Serialize and Deserialize BST

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

  7. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  9. [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

随机推荐

  1. 深入浅出mybatis之useGeneratedKeys参数用法

    目录 在settings元素中设置useGeneratedKeys参数 在xml映射器中配置useGeneratedKeys参数 在接口映射器中设置useGeneratedKeys参数 在MyBati ...

  2. Jenkins 子业务日志拆分分析方法

    需求 Jenkins日志打印内容很长,或者并发编译导致,日志内容不容易查看. 对于具体业务失败, 开发者希望看到具体业务自身的日志内容. 解法 tee 命令能够保证, shell命令执行的内容,即往控 ...

  3. Blender 2.78 突然卡顿

    之前一直好好的直到这两天打开就一直延迟半拍,重启重装无效. 解决方法: NVIDIA控制面板→管理3d设置→程序设置→选择blender,更改显卡为集成显卡→应用 亲测有效.

  4. CemtOS7更改yum网易 阿里云的yum源。

    一,鉴于用国外的Yum源,速度比较慢,所以想到将国外的yum源,改为国内的Yum源,著名的有网易 阿里云源.如何更改呢? 二,更改yum源为网易的. 首先备份/etc/yum.repos.d/Cent ...

  5. tp学习笔记1

    1.在tp中,model实际表示的只是一条记录,所以是个一维数组.所有记录组成的记录集,是用collection对象表示的. 2.model有一个静态方法get,使用:model::get(),返回的 ...

  6. 【转载】Jenkins安装以及邮件配置

    转载:http://www.nnzhp.cn/archives/590 Jenkins介绍 Jenkins是一个java开发的.开源的.非常好用持续集成的工具,它能帮我们实现自动化部署环境.测试.打包 ...

  7. JAVA进阶9

    间歇性混吃等死,持续性踌躇满志系列-------------第9天 1.使用throw语句抛出异常 在通常情况下,程序发生错误时系统会自动抛出异常,而有时希望程序自动抛出异常,可以使用throw语句来 ...

  8. CKEditor5 + vue2.0 自定义图片上传、highlight、字体等用法

    因业务需求,要在 vue2.0 的项目里使用富文本编辑器,经过调研多个编辑器,CKEditor5 支持 vue,遂采用.因 CKEditor5 文档比较少,此处记录下引用和一些基本用法. CKEdit ...

  9. Python-Django-Ajax进阶3

    1 中间件 -是什么? 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用不 ...

  10. sublime代码整理

    1. 2.输入: { "keys":["ctrl+shift+z"], (快捷键自定义,只要不冲突就可以了) "command":" ...