Serialize and Deserialize N-ary Tree

3 篇文章 0 订阅

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, the above tree may be serialized as [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14].

You do not necessarily need to follow the above suggested formats, there are many more different formats that work so please be creative and come up with different approaches yourself.

思路:做这个题目之前,先明白 Serialize and Deserialize Binary TreeSerialize and Deserialize BST 区别和异同,Serialize and Deserialize Binary Tree 因为是BT,没有相互之前的大小关系,所以要serialize full tree,连NULL都要serialize进去,但是BST不同,BST有左右的大小关系,所以只用serialize 有value的node就行,两个都是用queue来serialize,然后用queue来build。N-ary Tree跟BT不同的是,他有children;不能用preorder去serilize,只能用level order去serilize。不同的是,N-ary后面有child的size信息,我们也serilize进去,那么desrilize的时候,用两个queue,一个存当前node的queue,一个存node的child size信息,那么,如果size是n,那么后面2 * n个string,都是当前node的child;每个node都有自己的child信息;Serilize用level order一行一行的serilize,(跟BT不一样, BT是用preorder, currrent, left, right),deserilize也是一层一层的build;

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Codec {
    private String NULL = "NULL";
    private String DELIMITER = "#";
    // Encodes a tree to a single string.
    public String serialize(Node root) {
        StringBuilder sb = new StringBuilder();
        if(root == null) {
            sb.append(NULL);
            return sb.toString();
        }
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        
        while(!queue.isEmpty()) {
            Node node = queue.poll();
            int csize = node.children.size();
            sb.append(node.val).append(DELIMITER)
                .append(csize).append(DELIMITER);
            for(Node child: node.children) {
                queue.offer(child);
            }
        }
        return sb.toString();
    }
	
    // Decodes your encoded data to tree.
    public Node deserialize(String data) {
        if(data == null || data.equals(NULL)) {
            return null;
        }
        String[] splits = data.split(DELIMITER);
        Queue<Node> nqueue = new LinkedList<>();
        Queue<Integer> cqueue = new LinkedList<>();
        int index = 0;
        Node root = new Node(Integer.parseInt(splits[index++]));
        nqueue.offer(root);
        cqueue.offer(Integer.parseInt(splits[index++]));
        
        while(!nqueue.isEmpty()) {
            Node node = nqueue.poll();
            node.children = new ArrayList<>();
            int csize = cqueue.poll();
            for(int i = 0; i < csize; i++) {
                Node child = new Node(Integer.parseInt(splits[index++]));
                nqueue.offer(child);
                node.children.add(child);
                
                int childsize = Integer.parseInt(splits[index++]);
                cqueue.offer(childsize);
            }
        }
        return root;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值