[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 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.
思路
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叉树的更多相关文章
- LeetCode 428. Serialize and Deserialize N-ary Tree
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ 题目: Serialization is the ...
- [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 297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...
- [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 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- [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 ...
- [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树
原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...
- [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
随机推荐
- 集合,ArrayList
用集合代替数组: Console.Write("请输入人数:"); int renshu = int.Parse(Console.ReadLine()); ArrayList ch ...
- Linux下使用命令行配置IPMI
ipmitool是什么: 百度百科给的解释已经够用了,简单说就是“IPMI(Intelligent Platform Management Interface)即智能平台管理接口是使硬件管理具备“智能 ...
- DbUtil数据库连接
DbUtil数据库连接 package com.zjx.util; import java.sql.Connection; import java.sql.DriverManager; public ...
- 【381】python 获取列表中重复元素的索引值
参考:获取python的list中含有重复值的index方法_python_脚本之家 核心思想:建立字典,遍历列表,把列表中每个元素和其索引添加到字典里面 cc = [1, 2, 3, 2, 4] f ...
- 代码规范【经理培训内容记录】[有参考:http://kb.cnblogs.com/page/179593/]
一.命名规范 方法:所有首字母大写,如BloodControl; 类:所有首字母大写: 变量:第一个首字母小写,其他首字母大写:如bloodControl; 常量:全部字母大写,可用下划线分隔:如BL ...
- hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- LisView控件
用LisView控件在窗体中创建一个表,设置一个按钮,点击按钮, 将数据库中的表在这个控件中显示(LisView控件中表格式列名与数据库中一致) 首先使用控件将表的每一列创建好,加入一个按钮,如图,现 ...
- Jenkins 踩过的坑之再总结
在安装完jenkins后,linux中默认使用的jenkins这个用户,这时在构建完项目后我们需要执行一些shell命令时会出现没有权限的情况,导致构建失败,这里我们需要给jenkins用户相应的权限 ...
- whatweb工具
WhatWeb WhatWeb可以用来确定服务器使用的CMS.博客平台.统计分析软件包.JavaScript库等.这个工具有超过900个插件用来扫描目标. 使用方法: root@root:/pente ...
- 大型运输行业实战_day11_2_事务理论与实际生产配置事务管理
1.什么是事务(Transaction:tx) 数据库的某些需要分步完成,看做是一个整体(独立的工作单元),不能分割,要么整体成功,要么整体生效.“一荣俱荣,一损俱损”,最能体现事务的思想.案例:银行 ...