资讯专栏INFORMATION COLUMN

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

iamyoung001 / 1800人阅读

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/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元查看
<