offer2-48


序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。

示例 1:

img

输入:root = [1,2,3,null,null,4,5]
输出:[1,2,3,null,null,4,5]

示例 2:

输入:root = []
输出:[]

示例 3:

输入:root = [1]
输出:[1]

示例 4:

输入:root = [1,2]
输出:[1,2]

//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();

        sb.append("[");

        Queue<TreeNode> nodeQ = new LinkedList<>();

        if (root != null) {
            nodeQ.add(root);
            sb.append(root.val);

            while (!nodeQ.isEmpty()) {
                StringBuilder localSb = new StringBuilder();
                int nodeNum = nodeQ.size();
                for (int i = 0; i < nodeNum; i++) {
                    TreeNode node = nodeQ.poll();
                    localSb.append(",");
                    if (node.left == null) {
                        localSb.append("null");
                    } else {
                        localSb.append(node.left.val);
                        nodeQ.add(node.left);
                    }

                    localSb.append(",");
                    if (node.right == null) {
                        localSb.append("null");
                    } else {
                        localSb.append(node.right.val);
                        nodeQ.add(node.right);
                    }
                }
                if (!nodeQ.isEmpty()) {
                    sb.append(localSb.toString());
                }
            }
        }

        sb.append("]");

        return sb.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        data = data.substring(1, data.length() - 1);

        String[] dataArr = data.split(",");
        if (dataArr.length == 1) {
            if (dataArr[0].length() == 0) {
                return null;
            } else {
                return new TreeNode(Integer.valueOf(dataArr[0]));
            }
        } else {
            int index = 0;
            Queue<TreeNode> nodeQ = new LinkedList<>();
            TreeNode root = new TreeNode(Integer.valueOf(dataArr[index++]));
            nodeQ.add(root);

            while (index < dataArr.length) {
                int nodeNum = nodeQ.size();
                for (int i = 0; i < nodeNum; i++) {
                    TreeNode node = nodeQ.poll();
                    String left = dataArr[index++];
                    String right = dataArr[index++];

                    if (!"null".equals(left)) {
                        TreeNode leftNode = new TreeNode(Integer.valueOf(left));
                        node.left = leftNode;
                        nodeQ.add(leftNode);
                    }
                    if (!"null".equals(right)) {
                        TreeNode rightNode = new TreeNode(Integer.valueOf(right));
                        node.right = rightNode;
                        nodeQ.add(rightNode);
                    }
                }
            }

            return root;
        }

    }
}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));
//leetcode submit region end(Prohibit modification and deletion)

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !