标签:问题 enc 二叉树 定义 比较 信息 null 大小 效率
2018-06-16 18:53:36
序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程。反序列化顾名思义就是通过信息流对对象进行重建的过程。
一般来说序列化和反序列化有如下的作用:

public class SerializeandDeserializeTree {
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuffer sb = new StringBuffer();
        preOrder(root, sb);
        return sb.toString();
    }
    private void preOrder(TreeNode root, StringBuffer stringBuffer) {
        if (root == null) {
            stringBuffer.append("00000");
        }
        else {
            byte[] bytes = intToByte(root.val);
            stringBuffer.append(‘1‘).append((char) bytes[0]).append((char) bytes[1]).append((char) bytes[2]).append((char) bytes[3]);
            preOrder(root.left, stringBuffer);
            preOrder(root.right, stringBuffer);
        }
    }
    private byte[] intToByte(int val) {
        return new byte[]{
                (byte) (val >> 24),
                (byte) (val >> 16),
                (byte) (val >> 8),
                (byte) val
        };
    }
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        return helper(data.toCharArray(), new int[]{0});
    }
    private TreeNode helper(char[] data, int[] pos) {
        if (pos[0] >= data.length) return null;
        if (data[pos[0]] == ‘0‘) {
            pos[0] += 5;
            return null;
        }
        int val =
                (data[pos[0] + 1]) << 24 & 0xff000000 |
                (data[pos[0] + 2]) << 16 & 0x00ff0000 |
                (data[pos[0] + 3]) << 8 & 0x0000ff00 |
                (data[pos[0] + 4]) << 0 & 0x000000ff;
        pos[0] += 5;
        TreeNode root = new TreeNode(val);
        root.left = helper(data, pos);
        root.right = helper(data, pos);
        return root;
    }
}
二、 二叉搜索树的序列化和反序列化
问题描述:
 
问题求解:
public class SerializeandDeserializeBST {
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuffer sb = new StringBuffer();
        preOrder(root, sb);
        return sb.toString();
    }
    byte[] intToByte(int val) {
        return new byte[]{
                (byte)(val >> 24),
                (byte)(val >> 16),
                (byte)(val >> 8),
                (byte)val
        };
    }
    void preOrder(TreeNode root, StringBuffer sb) {
        if (root != null) {
            byte[] tmp = intToByte(root.val);
            sb.append((char) tmp[0]).append((char) tmp[1]).append((char) tmp[2]).append((char) tmp[3]);
            preOrder(root.left, sb);
            preOrder(root.right, sb);
        }
    }
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        return helper(data.toCharArray(), new int[]{0}, Integer.MIN_VALUE, Integer.MAX_VALUE);
    }
    private TreeNode helper(char[] data, int[] pos, int low, int high) {
        if(pos[0] >= data.length) return null;
        int val =
                data[pos[0] + 0] << 24 & (0xff000000) |
                data[pos[0] + 1] << 16 & (0x00ff0000) |
                data[pos[0] + 2] << 8 & (0x0000ff00) |
                data[pos[0] + 3] << 0 & (0x000000ff);
        if(val < low || val > high) return null;
        TreeNode root = new TreeNode(val);
        pos[0] += 4;
        root.left = helper(data, pos, low, val);
        root.right = helper(data, pos, val, high);
        return root;
    }
    int byteToInt(byte[] bytes) {
        return
                bytes[0] & (0xff000000) |
                bytes[1] & (0x00ff0000) |
                bytes[2] & (0x0000ff00) |
                bytes[3] & (0x000000ff);
    }
}
标签:问题 enc 二叉树 定义 比较 信息 null 大小 效率
原文地址:https://www.cnblogs.com/TIMHY/p/9191122.html