Serialize and Deserialize Binary Tree

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 a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary 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 tree

    1
   / \
  2   3
     / \
    4   5
as  "[1,2,3,null,null,4,5]" , just the same as  how LeetCode OJ serializes a binary tree . You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

按照preorder的方法encode (1 2 n n 3 4 n n 5 n n)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {
    private class MutableInt{
        public int val;
        public MutableInt(int val){
            this.val = val;
        }
    }

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        if(root == null)
            return null;
        StringBuffer result = serializeHelper(root);
        return result.toString();
    }
    
    private StringBuffer serializeHelper(TreeNode root){
        if(root == null)
            return new StringBuffer("n");
        StringBuffer result = new StringBuffer(String.valueOf(root.val));
        result.append(" ");
        result.append(serializeHelper(root.left));
        result.append(" ");
        result.append(serializeHelper(root.right));
        return result;
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if(data == null)
            return null;
        return deserializeHelper(data.split(" "), new MutableInt(0));
    }
    
    private TreeNode deserializeHelper(String[] data, MutableInt index){
        int indexVal = index.val;
        if(data[indexVal].equals("n")){
            index.val = indexVal + 1;
            return null;
        }
        
        TreeNode result = new TreeNode(Integer.parseInt(data[indexVal]));
        index.val = indexVal + 1;
        result.left = deserializeHelper(data, index);
        result.right = deserializeHelper(data, index);
        return result;
    }
}


按照 tree mining的方法encode,稍加改变可作为N-ary tree encoding.但是每个node的children里若有null,则null children之后的children必须都为null。即不能有null出现在中间

class Codec {
    private class MutableInt{
        public int val;
        public MutableInt(int val){
            this.val = val;
        }
    }

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        if(root == null)
            return null;
        StringBuffer result = serializeHelper(root);
        return result.toString();
    }

    private StringBuffer serializeHelper(TreeNode root){
        if(root.left == null && root.right == null)
            return new StringBuffer(String.valueOf(root.val) + " n");
        StringBuffer result = new StringBuffer(String.valueOf(root.val));
        result.append(" ");
        result.append(serializeHelper(root.left));
        if(root.right != null) {
            result.append(" ");
            result.append(serializeHelper(root.right));
        }
        result.append(" n");
        return result;
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if(data == null)
            return null;
        return deserializeHelper(data.split(" "), new MutableInt(0));
    }

    private TreeNode deserializeHelper(String[] data, MutableInt index){
        int indexVal = index.val;
        TreeNode result = new TreeNode(Integer.parseInt(data[indexVal]));
        index.val = indexVal + 1;
        if(data[index.val].equals("n")) {
            index.val += 1;
            return result;
        }
        result.left = deserializeHelper(data, index);
        if(data[index.val].equals("n")) {
            index.val += 1;
            return result;
        }
        result.right = deserializeHelper(data, index);
        return result;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值