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之路】特别篇--RabbitMQ

    RabbitMQ 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议. AMQP协议是一种基于网络的消息传输协议,它能够在应用或组织之 ...

  2. luogu 1876 开灯 约数+打表

    打表后发现答案都是完全平方数,直接输出即可. #include <cstdio> #include <algorithm> using namespace std; int m ...

  3. k8s集群管理注意要点【持续更新】

    1.编写pod yaml文件时绑定调度标签,必须要给指定节点绑定标签,否则无法调度到指定节点上,报错: Events: Type Reason Age From Message ---- ------ ...

  4. RabbitAdmin

    RabbitAdmin底层实现就是从Spring容器中获取Exchange.Binding.Routingkey以及Queue的@声明然后使用RabbitTemplate的execute方法执行对应的 ...

  5. Vue_(组件)实例生命周期钩子

    Vue生命周期中文文档 传送门 Vue生命周期:Vue实例从创建到销毁的过程,称为Vue的生命周期: Vue生命周期钩子:又称为Vue生命周期钩子方法/函数,是Vue为开发者提供的方法,我们可以通过这 ...

  6. Xpath中text(),string(),data()的区别

    摘要: 在XPath中,经常使用text()和string(),而我一般都是想到哪个用哪个,究竟他们之间有什么不同,没有在意过. 本质区别 text()是一个node test,而string()是一 ...

  7. 回调函数(callback) python / c++ 演示

    什么是回调函数? 我们绕点远路来回答这个问题. 编程分为两类:系统编程(system programming)和应用编程(application programming).所谓系统编程,简单来说,就是 ...

  8. 浅析SPDY

    1.什么是SPDY? 简单地说,在SSL层上增加一个SPDY会话层,以在一个TCP连接支持并发的HTTP请求.也就是他能通过复用仅仅一条(或几条)TCP连接,在客户端与服务器间发送几十个请求或回应. ...

  9. Ubuntu16.04配置vncserver后 导致重复进入登陆界面,无法进入桌面的问题

      1.在配置vncserver的时候,可能导致该用户不能正常登录桌面. 2.问题现象:正确输入密码,系统无法进入桌面,闪回到登录界面. 3.在登录界面按ctrl+Alt+F1,进入虚拟控制台(输入r ...

  10. storm备忘

    [命令]storm rebalance topology-name [-w wait-time-secs] [-n new-num-workers] [-e component=parallelism ...