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 ...
随机推荐
- JVM(八),垃圾回收标记算法
八.垃圾回收标记算法 1.对象被判定成垃圾的标准 没有被其他对象引用 2.判断对象是否为垃圾的算法 (1)引用计数法 优点and缺点 (2)可达性分析算法
- matlab函数 bsxfun浅谈(转载)
关于matlab函数 bsxfun本文来源:http://blog.sina.com.cn/s/blog_9e67285801010ttn.html a=[1 2 3];b=[1 2 3]';c=bs ...
- Paint the Digits
C - Paint the Digits 思路:这道题就只需要利用单调栈,将整个数组扫一遍,求得的最后的栈内元素(要求全部小于非栈内元素)的颜色为1,其余为2 那么怎么实现呢?求最后的栈内元素(要求全 ...
- 关于lower_bound()和upper_bound()
关于lower_bound()和upper_bound(): 参考:关于lower_bound( )和upper_bound( )的常见用法 注意:查找的数组必须要是排好序的.因为,它们查找的方式也是 ...
- centos6升级系统内核
1.升级系统内核查看内核版本: uname -r 2.6.32-573.8.1.el6.x86_64 导入elrepo的key: rpm --import https://www.elrepo.org ...
- C++入门经典-例9.3-类模板,简单类模板
1:使用template关键字不但可以定义函数模板,而且可以定义类模板.类模板代表一族类,它是用来描述通用数据类型或处理方法的机制,它使类中的一些数据成员和成员函数的参数或返回值可以取任意数据类型.类 ...
- LeetCode 137. 只出现一次的数字 II(Single Number II)
题目描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...
- window 下要运行php,需要编辑php环境变量
参考:https://blog.csdn.net/zhezhebie/article/details/72765262
- JScript 对字符串、数组处理的常用方法
1.anchor 方法 在对象中的指定文本两端放置一个有 NAME 属性的 HTML 锚点. strVariable.anchor(anchorString) var strVariable ...
- 一句话说明Facbook React证书的矛盾点
这项专利授权说,如果您要使用我们根据这项授权发布的软件,假如您因为专利侵权而提起诉讼,您将失去我们的专利许可.