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.

思路

1.  preorder recursive traversal

2. add number of children after root val, in order to know when to terminate

1 3 3 2 5 0 6 0 2 0 4 0

代码

 /*
// 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> list = new LinkedList<>();
buildString(root, list);
return String.join(",", list);
} private void buildString(Node root, List<String> list) {
if (root == null) return; list.add(String.valueOf(root.val));
list.add(String.valueOf(root.children.size()));
for (Node child : root.children) {
buildString(child, list);
} } // Decodes your encoded data to tree.
public Node deserialize(String data) {
if (data.length() == 0) return null;
String[] strArr = data.split(",");
Queue<String> queue = new LinkedList<>();
Collections.addAll(queue, strArr);
return buildTree(queue);
} private Node buildTree(Queue<String> queue) {
// match the given constructor form
Node root = new Node();
root.val = Integer.parseInt(queue.poll());
int size = Integer.parseInt(queue.poll());
root.children = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
root.children.add(buildTree(queue));
}
return root;
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

[leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树的更多相关文章

  1. LeetCode 428. Serialize and Deserialize N-ary Tree

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ 题目: Serialization is the ...

  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. 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树

    原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...

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

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

随机推荐

  1. 在IDEA中修改项目的名称

  2. TWebBrowser禁止弹出Alert对话框

    以前介绍过通过编写Webbrowser1的OnDocumentComplete事件响应代码可以拦截网页弹出的Alert等对话框,代码如下: procedure TForm1.WebBrowser1Do ...

  3. Oracle 学习总结 - 问题诊断

    搜集常用诊断sql https://blog.csdn.net/yangshangwei/article/details/52449489 lock相关: 1. 查看lock, 打开两个事物,事物1更 ...

  4. JavaScript随机生成信用卡卡号的方法

    这段JS代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考,请不要用于非法用途,否则后果自负. var visaPrefixList = new Array( "4 ...

  5. open read split

    open  来打开文件, 其具体表现为 open('文件名或路径', 'r or w or other', 位置?) 其生成一个文件类型的对象 file object. 可写做 FILENAME = ...

  6. MyBatis对入参对象的属性空判断

      <!-- 查询学生list,like姓名 -->   <select id="getStudentListLikeName" parameterType=&q ...

  7. overload重载

    方法的重载 /** * 重载 overload * @author Administrator *同一个类,同一个方法 *不同:参数列表不同(类型,个数,顺序) 只和 参数列表有关 * 跟 返回值 和 ...

  8. effective C++学习三(仅供个人学习记录,本文摘录effective C++)

    条款 3:尽量用 new 和 delete 而不用 malloc 和 free  把 new和 delete 与malloc 和 free 混在一起用也是个坏想法.对一个用 new 获取来的指针调用 ...

  9. javascript中 Array.prototype.slice的用法.

    首先看到 www.w3school.cn上的解释:http://www.w3school.com.cn/jsref/jsref_slice_array.asp 定义和用法 slice() 方法可从已有 ...

  10. 最小生成树二·Kruscal算法

    描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用了——但是幸运的是,经过计算机的分析,小Hi已经筛选出了一些比较适合建造道路的路线,这个数量并没有特别的大. 所以问题变成 ...