Java教程

剑指 Offer 37. 序列化二叉树

本文主要是介绍剑指 Offer 37. 序列化二叉树,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

请实现两个函数,分别用来序列化和反序列化二叉树。

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

提示:输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

深度优先遍历(先序)

import java.util.Arrays;
import java.util.LinkedList;

public class Codec {
    public String serialize(TreeNode root) {
        return rserialize(root);
    }

    public TreeNode deserialize(String data) {
        String[] dataArray = data.split(",");
        LinkedList<String> dataList = new LinkedList<>(Arrays.asList(dataArray));
        return rdeserialize(dataList);
    }

    public String rserialize(TreeNode root) {
        if (root == null) {
            return "#";
        }
        return root.val + "," + rserialize(root.left) + "," + rserialize(root.right);
    }

    public TreeNode rdeserialize(LinkedList<String> dataList) {
        if ("#".equals(dataList.peekFirst())) {
            dataList.pollFirst();
            return null;
        }
        TreeNode root = new TreeNode(Integer.parseInt(dataList.pollFirst()));
        root.left = rdeserialize(dataList);
        root.right = rdeserialize(dataList);
        return root;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

深度优先遍历(后序)

import java.util.Arrays;
import java.util.LinkedList;

public class Codec {
    public String serialize(TreeNode root) {
        return rserialize(root);
    }

    public TreeNode deserialize(String data) {
        String[] dataArray = data.split(",");
        LinkedList<String> dataList = new LinkedList<>(Arrays.asList(dataArray));
        return rdeserialize(dataList);
    }

    public String rserialize(TreeNode root) {
        if (root == null) {
            return "#";
        }
        return rserialize(root.left) + "," + rserialize(root.right) + "," + root.val;
    }

    public TreeNode rdeserialize(LinkedList<String> dataList) {
        if ("#".equals(dataList.peekLast())) {
            dataList.pollLast();
            return null;
        }
        TreeNode root = new TreeNode(Integer.parseInt(dataList.pollLast()));
        root.right = rdeserialize(dataList);
        root.left = rdeserialize(dataList);
        return root;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

广度优先遍历(层序)

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

public class Codec {
    public String serialize(TreeNode root) {
        StringBuilder ans = new StringBuilder();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node == null) {
                ans.append("#").append(",");
            } else {
                ans.append(node.val).append(",");
                queue.offer(node.left);
                queue.offer(node.right);
            }
        }

        return ans.toString();
    }

    private TreeNode build(String data) {
        if ("#".equals(data)) {
            return null;
        }
        return new TreeNode(Integer.parseInt(data));
    }

    public TreeNode deserialize(String data) {
        if (data == null || data.length() == 0) {
            return null;
        }
        String[] dataArray = data.split(",");
        LinkedList<String> dataList = new LinkedList<>(Arrays.asList(dataArray));
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = build(dataList.pollFirst());
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node != null) {
                node.left = build(dataList.pollFirst());
                queue.offer(node.left);
                node.right = build(dataList.pollFirst());
                queue.offer(node.right);
            }
        }
        return root;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}
这篇关于剑指 Offer 37. 序列化二叉树的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!