算法19:LeetCode_二叉树序列化与反序列化(层序)

 本题为链接为https://leetcode.cn/problems/serialize-and-deserialize-binary-tree

想要搞懂本题,请先阅读我之前写的关于二叉树层序遍历文章算法8:LeetCode_二叉树的层序遍历_chen_yao_kerr的博客-CSDN博客

 

本体在可在LeetCode直接进行测试,但是需要替换变量 SEPARATOR 为 “,”. 因为这是我优化代码提取出来的一个公共的东西,写太多次怕出错.

package code03.二叉树_02;

import java.util.LinkedList;
import java.util.Queue;

/**
 * https://leetcode.cn/problems/serialize-and-deserialize-binary-tree/
 *
 * 二叉树按层序列化 与 反序列化
 *
 */
public class Code01_SerializeAndReconstructTree
{
    static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
    }

    public static final String SEPARATOR = ",";

    public String serialize(TreeNode root)
    {
        //边界值判断
        if (root == null) {
            return "";
        }

        //逐层搜集,首先收集根节点,并存入队列。印证思路第1步
        StringBuffer sb = new StringBuffer();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        sb.append(root.val);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node.left != null) {
                queue.add(node.left);
                sb.append(SEPARATOR).append(node.left.val);
            }
            else {
                sb.append(SEPARATOR).append("null");
            }

            if (node.right != null) {
                queue.add(node.right);
                sb.append(SEPARATOR).append(node.right.val);
            }
            else {
                sb.append(SEPARATOR).append("null");
            }
        }

        return sb.toString();
    }

    public TreeNode generateNode (String str)
    {
        if ("null".equals(str)) {
            return null;
        }
        return new TreeNode(Integer.valueOf(str));
    }

    public TreeNode deserialize(String data)
    {
        if (data == null || data.isEmpty()) {
            return null;
        }

        Queue<String> queue = new LinkedList<>();
        String[] arr = data.split(SEPARATOR);
        for (int i = 0; i < arr.length; i++) {
            queue.add(arr[i]);
        }

        Queue<TreeNode> queueNode = new LinkedList<>();
        TreeNode node = generateNode(queue.poll());
        queueNode.add(node);
        TreeNode cur = null;
        while (!queueNode.isEmpty()) {
            cur = queueNode.poll();
            if (cur != null) {
                cur.left =  generateNode(queue.poll());
                cur.right = generateNode(queue.poll());
                queueNode.add(cur.left);
                queueNode.add(cur.right);
            }
        }
        return node;
    }

    public static void main(String[] args)
    {
        TreeNode tree = new TreeNode(1);
        tree.left = new TreeNode(2);
        tree.right = new TreeNode(3);
        tree.left.left = new TreeNode(4);
        tree.right.right = new TreeNode(5);
        tree.left.left.left = new TreeNode(6);

        Code01_SerializeAndReconstructTree test = new Code01_SerializeAndReconstructTree();
        String str = test.serialize(tree);
        System.out.println(str);

        TreeNode node = test.deserialize(str);
        System.out.println("===========测试完毕,debug查看node================");
    }
}

 

 
posted @ 2023-02-22 10:58  街头小瘪三  阅读(17)  评论(0编辑  收藏  举报