【LeetCode笔记】剑指Offer 37. 序列化二叉树(Java、二叉树、序列化、BFS、队列)

题目描述

  • 这道题涉及到不少 String、StringBuilder、Integer的转换、处理。
    在这里插入图片描述

思路 && 代码

  • 序列化:迭代进行一个层序遍历,逐个加入结果字符串中。
  • 反序列化:根据序列化得到的结果字符串,同样是借助队列,进行层序遍历来构造出二叉树。
  • 总的来说,是需要多敲几遍的题(String等类的用法)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {
    // 知识点:String、StringBuilder、parseInt的处理

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        if(root == null) {
            return "[]";
        }

        StringBuilder res = new StringBuilder("[");
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        // 层序遍历 BFS(迭代) 
        while(!queue.isEmpty()) {
            TreeNode temp = queue.poll();
            if(temp != null) {
                // 当前值加入 res,子结点加入 queue
                res.append(temp.val).append(",");
                queue.add(temp.left);
                queue.add(temp.right);
            }
            // 空结点 情况
            else {
                res.append("null,");
            }
        }
        // 删除末尾的','
        res.delete(res.length() - 1, res.length());
        res.append("]");
        return res.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if(data.equals("[]")) {
            return null;
        }

        // 1. init
        String[] vals = data.substring(1, data.length() - 1).split(",");
        TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        // 2. deserialize
        for(int i = 1; !queue.isEmpty(); i += 2) {
            TreeNode temp = queue.poll();
            // 左结点判断
            if(!vals[i].equals("null")) {
                temp.left = new TreeNode(Integer.parseInt(vals[i]));
                queue.add(temp.left);
            }
            // 右结点判断
            if(!vals[i + 1].equals("null")) {
                temp.right = new TreeNode(Integer.parseInt(vals[i + 1]));
                queue.add(temp.right);
            }
        }
        return root;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

二刷

  • 序列化格式:逗号’,'作为分割符,“null” 作为空节点。按照层序遍历序列化
  • 注意:序列化、反序列都需要队列辅助
public class Codec {
    public String serialize(TreeNode root) {
        if(root == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            TreeNode temp = queue.poll();
            if(temp == null) {
                sb.append("null,");
            }
            else {
                sb.append(temp.val).append(',');
                queue.add(temp.left);
                queue.add(temp.right);
            }
        }
        return sb.toString();
    }

    public TreeNode deserialize(String data) {
        if(data.length() == 0) {
            return null;
        }

        String[] vals = data.split(",");
        TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
        LinkedList<TreeNode> queue = new LinkedList<>(); // 还是需要辅助队列
        queue.add(root);
        for(int i = 1; i < vals.length; i += 2) {
            TreeNode temp = queue.poll();
            if(!vals[i].equals("null")) {
                temp.left = new TreeNode(Integer.parseInt(vals[i]));
                queue.add(temp.left);
            }
            if(!vals[i + 1].equals("null")) {
                temp.right = new TreeNode(Integer.parseInt(vals[i + 1]));
                queue.add(temp.right);
            }
        }
        return root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值