Problem
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.
class Codec { // Encodes a tree to a single string. public String serialize(Node root) { if (root == null) return null; Listlist = new LinkedList<>(); serializeHelper(root, list); return String.join(",", list); } private void serializeHelper(Node root, List list) { if (root == null) return; list.add(String.valueOf(root.val)); list.add(String.valueOf(root.children.size())); for (Node child: root.children) { serializeHelper(child, list); } } // Decodes your encoded data to tree. public Node deserialize(String data) { if (data == null || data.length() == 0) return null; String[] values = data.split(","); Queue queue = new LinkedList<>(Arrays.asList(values)); return deserializeHelper(queue); } private Node deserializeHelper(Queue queue) { Node root = new Node(); root.val = Integer.valueOf(queue.poll()); int size = Integer.valueOf(queue.poll()); root.children = new ArrayList<>(); for (int i = 0; i < size; i++) { root.children.add(deserializeHelper(queue)); } return root; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/71861.html
Problem 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 li...
摘要:題目大意將二叉樹序列化,返回序列化的,和反序列化還原。解題思路技巧在于將記錄為便于將來判斷。的思想將每一層記錄下來,反序列化時也按照層級遍歷的方法依次設置為上一個里面的元素的左孩子和右孩子。變種,可以要求輸出一個,而不是 LeetCode 297. Serialize and Deserialize Binary Tree 題目大意: 將二叉樹序列化,返回序列化的String,和反序列...
摘要:因此題目中的例子的中序遍歷結果為。但是,標準的中序遍歷并不能使我們將該結果反構造成一棵樹,我們丟失了父節點和子節點之間的關系。這里我們也可以明顯的看出來,中序遍歷需要保存的空值遠遠多于廣度優先遍歷。 題目要求 Serialization is the process of converting a data structure or object into a sequence of ...
摘要:題目要求將二叉搜索樹序列化和反序列化,序列化是指將樹用字符串的形式表示,反序列化是指將字符串形式的樹還原成原來的樣子。假如二叉搜索樹的節點較多,該算法將會占用大量的額外空間。 題目要求 Serialization is the process of converting a data structure or object into a sequence of bits so that...
摘要:題目鏈接題目分析按層遍歷叉樹。思路以層數為鍵,塞入當前節點的值。最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D55 429. N-ary Tree Level Order Traversal 題目鏈接 429. N-ary Tree Level Order Traversal 題目分析 按層遍歷N叉樹。 思路 以層數為鍵,塞入當前節點的值。 遞歸遍歷即可。 最終代碼
閱讀 3585·2023-04-26 01:43
閱讀 2972·2021-10-14 09:42
閱讀 5404·2021-09-30 09:59
閱讀 2172·2021-09-04 16:40
閱讀 1208·2019-08-30 15:52
閱讀 822·2019-08-29 17:09
閱讀 1993·2019-08-26 13:37
閱讀 3432·2019-08-26 10:20