成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

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

iamyoung001 / 1905人閱讀

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

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71861.html

相關(guān)文章

  • [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

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

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

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

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

    摘要:題目要求將二叉搜索樹序列化和反序列化,序列化是指將樹用字符串的形式表示,反序列化是指將字符串形式的樹還原成原來的樣子。假如二叉搜索樹的節(jié)點較多,該算法將會占用大量的額外空間。 題目要求 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

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

    libxd 評論0 收藏0

發(fā)表評論

0條評論

iamyoung001

|高級講師

TA的文章

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