国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 428. Serialize and Deserialize N-ary Tr

iamyoung001 / 1896人閱讀

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.

Solution
class Codec {

    // Encodes a tree to a single string.
    public String serialize(Node root) {
        if (root == null) return null;
        List list = 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

相關文章

  • [LeetCode] 297. Serialize and Deserialize Binary T

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

    MRZYD 評論0 收藏0
  • LeetCode 297. Serialize and Deserialize Binary Tre

    摘要:題目大意將二叉樹序列化,返回序列化的,和反序列化還原。解題思路技巧在于將記錄為便于將來判斷。的思想將每一層記錄下來,反序列化時也按照層級遍歷的方法依次設置為上一個里面的元素的左孩子和右孩子。變種,可以要求輸出一個,而不是 LeetCode 297. Serialize and Deserialize Binary Tree 題目大意: 將二叉樹序列化,返回序列化的String,和反序列...

    cc17 評論0 收藏0
  • leetcode297. Serialize and Deserialize Binary Tree

    摘要:因此題目中的例子的中序遍歷結果為。但是,標準的中序遍歷并不能使我們將該結果反構造成一棵樹,我們丟失了父節點和子節點之間的關系。這里我們也可以明顯的看出來,中序遍歷需要保存的空值遠遠多于廣度優先遍歷。 題目要求 Serialization is the process of converting a data structure or object into a sequence of ...

    desdik 評論0 收藏0
  • leetcode449. Serialize and Deserialize BST

    摘要:題目要求將二叉搜索樹序列化和反序列化,序列化是指將樹用字符串的形式表示,反序列化是指將字符串形式的樹還原成原來的樣子。假如二叉搜索樹的節點較多,該算法將會占用大量的額外空間。 題目要求 Serialization is the process of converting a data structure or object into a sequence of bits so that...

    Honwhy 評論0 收藏0
  • Leetcode PHP題解--D55 429. N-ary Tree Level Order Tr

    摘要:題目鏈接題目分析按層遍歷叉樹。思路以層數為鍵,塞入當前節點的值。最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D55 429. N-ary Tree Level Order Traversal 題目鏈接 429. N-ary Tree Level Order Traversal 題目分析 按層遍歷N叉樹。 思路 以層數為鍵,塞入當前節點的值。 遞歸遍歷即可。 最終代碼

    libxd 評論0 收藏0

發表評論

0條評論

iamyoung001

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<