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:
N
is in the range of[1, 1000]
- 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的更多相关文章
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Serialize and Deserialize Binary Tree
Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
- Serialize and Deserialize Binary Tree
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- 297. Serialize and Deserialize Binary Tree
题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- jquery blur()函数 语法
jquery blur()函数 语法 作用:当元素失去焦点时发生 blur 事件.blur() 函数触发 blur 事件,或者如果设置了 function 参数,该函数也可规定当发生 blur 事件时 ...
- mysql8.0.11安装
1.下载mysql-->下载 2.解压 3.添加my.ini配置文件 [mysqld]# 设置3306端口port=3306# 设置mysql的安装目录basedir=C:\wnmp\mysql ...
- Python实现telnet命令测试防火墙
Python实现telnet命令测试防火墙 telnet主要用于测试主机端口是否开通 ping主要是用来测试网络是否畅通和主机是否正在使用 使用Python实现Telnet测试主机端口是否开通的功能. ...
- JavaWeb_(Hibernate框架)Hibernate中重要的api
Hibernate中重要的api Configuration SessionFactory Session(重点) Transaction 在Dao层中UserDao.java使用Hibernate向 ...
- Android_activity实现一个简单的新建联系表
项目展示: 第一个Activity用于显示联系人信息 第二个Activity输入联系人信息 要求: 运行“新建联系人”程序,结果如下图所示: 点击“新建联系人”按钮,打开输入信息界面并输入姓名.公司. ...
- nmap脚本nse的使用
nmap脚本(nse)使用总结 0x01 nmap按脚本分类扫描 nmap脚本主要分为以下几类,在扫描时可根据需要设置--script=类别这种方式进行比较笼统的扫描: auth: 负责处理鉴权证书( ...
- Android学习_数据持久化
数据持久化:将内存中的瞬时数据存储到设备中 1. 文件存储 存储一些简单的文本数据或二进制数据. 核心:Context类提供的openFileOutput()和openFileInput()方法,然后 ...
- [题解] [HNOI2014] 世界树
题面 [HNOI2014]世界树 题解 从数据范围很容易看出是个虚树DP(可惜看出来了也还是不会做) 虚树大家应该都会, 不会的话自己去搜吧, 我懒得讲了, 我们在这里只需要考虑如何DP即可 首先我们 ...
- Okhttp源码分析--基本使用流程分析
Okhttp源码分析--基本使用流程分析 一. 使用 同步请求 OkHttpClient okHttpClient=new OkHttpClient(); Request request=new Re ...
- hearthbuddy中的Class276
构造函数 需要注意的是this.intptr_0 = this.method_18("mono.dll"); 所以,这个类里面的操作,最后是和mono.dll相关的 interna ...