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:

  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.

分析:下面这种方法

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     public int val;
 5     public List<Node> children;
 6  
 7     public Node() {}
 8  
 9     public Node(int _val,List<Node> _children) {
10         val = _val;
11         children = _children;
12     }
13 };
14 */
15 class Codec {
16  
17     // Encodes a tree to a single string.
18     public String serialize(Node root) {
19         if (root == null) return "";
20          
21         Queue<Node> que = new LinkedList<>();
22         StringBuilder sb = new StringBuilder();
23         sb.append(Integer.toString(root.val)).append(",#,");
24         que.add(root);
25          
26         while (!que.isEmpty()) {
27             Node node = que.poll();
28             for (Node n : node.children) {
29                 sb.append(Integer.toString(n.val)).append(",");
30                 que.add(n);
31             }
32             sb.append("#,");
33         }
34          
35         return sb.toString();
36     }
37  
38     // Decodes your encoded data to tree.
39     public Node deserialize(String data) {
40         if (data.length() == 0) return null;
41         String[] s = data.split(",");
42  
43         Queue<Node> que = new LinkedList<>();
44         Node root = new Node(Integer.parseInt(s[0]), new ArrayList<Node>());
45         que.add(root);
46         int i = 1;
47          
48         while (!que.isEmpty()) {
49             Node node = que.poll();
50             i++;
51             while (!s[i].equals("#")) {
52                 Node c = new Node(Integer.parseInt(s[i]), new ArrayList<>());
53                 node.children.add(c);
54                 que.add(c);
55                 i++;
56             }
57         }
58          
59         return root;
60     }
61 }
62  
63 // Your Codec object will be instantiated and called as such:
64 // Codec codec = new Codec();
65 // codec.deserialize(codec.serialize(root));

 

posted @ 2019-07-06 11:56  北叶青藤  阅读(310)  评论(0编辑  收藏  举报