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.

分析:下面这种方法

 /*
// 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) {
if (root == null) return ""; Queue<Node> que = new LinkedList<>();
StringBuilder sb = new StringBuilder();
sb.append(Integer.toString(root.val)).append(",#,");
que.add(root); while (!que.isEmpty()) {
Node node = que.poll();
for (Node n : node.children) {
sb.append(Integer.toString(n.val)).append(",");
que.add(n);
}
sb.append("#,");
} return sb.toString();
} // Decodes your encoded data to tree.
public Node deserialize(String data) {
if (data.length() == ) return null;
String[] s = data.split(","); Queue<Node> que = new LinkedList<>();
Node root = new Node(Integer.parseInt(s[]), new ArrayList<Node>());
que.add(root);
int i = ; while (!que.isEmpty()) {
Node node = que.poll();
i++;
while (!s[i].equals("#")) {
Node c = new Node(Integer.parseInt(s[i]), new ArrayList<>());
node.children.add(c);
que.add(c);
i++;
}
} return root;
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

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

  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. Serialize and Deserialize Binary Tree

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  5. 297. Serialize and Deserialize Binary Tree

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

  6. LeetCode OJ 297. Serialize and Deserialize Binary Tree

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

  7. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

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

  8. [LeetCode] 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 ...

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

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

随机推荐

  1. Python身份运算符

    运算符 描述 实例 is is 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False is not ...

  2. JVM(四),什么是反射

    四.什么是反射 1.反射理论 2.反射实践 (1)创建Robot类 public class Robot { private String name; public void sayHi(String ...

  3. IIS+上传4G文件

    最近在学习百度的开源上传组件WebUploader,写了一些示例以记录.WebUploader的缺点是没有一个比较好的现成的界面,这个界面需要自己去实现.自由度高了一些. WebUploader是由B ...

  4. luogu 1414 又是毕业季II 约数相关

    感觉好水呀~ Code: #include <cstdio> #include <algorithm> #define N 1000005 #define setIO(s) f ...

  5. Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...

  6. HDU - 6183 暴力,线段树动态开点,cdq分治

    B - Color itHDU - 6183 题目大意:有三种操作,0是清空所有点,1是给点(x,y)涂上颜色c,2是查询满足1<=a<=x,y1<=b<=y2的(a,b)点一 ...

  7. [题解] [JLOI2013] 卡牌游戏

    题面 题解 概率dp, 应该做得还是比较少的 设\(f[i][j]\)为该圈有\(i\)人时, 第\(j\)个人最后胜利的概率 枚举选择第几张卡牌, 设其值为\(card[k]\), 那么被淘汰的则是 ...

  8. 将maven项目发布到私服

    1.局域网私服 首先保证已经安装配置好了局域网的私服(具体私服的搭建可以查看相关资料) 私服页面访问地址:http://192.168.0.110:8081/nexus,使用admin登录,默认的用户 ...

  9. win7安装Elasticsearch和Elasticsearch-Head插件

    1.环境搭建 1)Java环境搭建可以参考相关的资料,这里不做详细介绍 2)nodejs环境搭建 到官方网站下载相应的zip包:https://nodejs.org/dist/v8.9.1/node- ...

  10. 【学习】eclipse自动提示+自动补全

    解决代码的自动提示问题: 1.打开 Eclipse -> Window -> Perferences 2.找到Java 下的 Editor 下的 Content Assist , 右边出现 ...