加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Serialize and Deserialize N-ary Tree

发布时间:2020-12-14 04:42:26 所属栏目:大数据 来源:网络整理
导读: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 anot

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));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读