[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

思路

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

代码

 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  // Encodes a tree to a single string.
17     public String serialize(Node root) {
18         List<String> list = new LinkedList<>();
19         buildString(root, list);
20         return String.join(",", list);
21     }
22
23     private void buildString(Node root, List<String> list) {
24         if (root == null) return;
25
26         list.add(String.valueOf(root.val));
27         list.add(String.valueOf(root.children.size()));
28         for (Node child : root.children) {
29             buildString(child, list);
30         }
31
32     }
33
34     // Decodes your encoded data to tree.
35     public Node deserialize(String data) {
36         if (data.length() == 0) return null;
37         String[] strArr = data.split(",");
38         Queue<String> queue = new LinkedList<>();
39         Collections.addAll(queue, strArr);
40         return buildTree(queue);
41     }
42
43     private Node buildTree(Queue<String> queue) {
44         // match the given constructor form
45         Node root = new Node();
46         root.val = Integer.parseInt(queue.poll());
47         int size = Integer.parseInt(queue.poll());
48         root.children = new ArrayList<>(size);
49         for (int i = 0; i < size; i++) {
50             root.children.add(buildTree(queue));
51         }
52         return root;
53     }
54 }
55
56
57 // Your Codec object will be instantiated and called as such:
58 // Codec codec = new Codec();
59 // codec.deserialize(codec.serialize(root));

原文地址:https://www.cnblogs.com/liuliu5151/p/9897169.html

时间: 2024-08-02 08:38:09

[leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树的相关文章

[LeetCode][JavaScript]Serialize and Deserialize Binary Tree

Serialize and Deserialize Binary 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 reconstruct

[leetcode]297. Serialize and Deserialize Binary 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 comput

[Lintcode]7. Serialize and Deserialize Binary Tree/[Leetcode]297. Serialize and Deserialize Binary Tree

7. Serialize and Deserialize Binary Tree/297. Serialize and Deserialize Binary Tree 本题难度: Medium/Hard Topic: Binary Tree Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called '

LeetCode 297: Serialize and Deserialize Binary Tree

1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Codec { 11 12 // Encodes a tree to a single string. 13 public Str

Leetcode: Serialize and Deserialize Binary 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 comput

LeetCode——Serialize and Deserialize Binary Tree

Description: 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 a

[LeetCode] Serialize and Deserialize Binary 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 comput

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 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 comput

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree Total Accepted: 47713 Total Submissions: 149430 Difficulty: Hard Contributors: Admin Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in