其他分享
首页 > 其他分享> > 剑指 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;
    }
}

标签:TreeNode,LinkedList,dataList,37,二叉树,return,序列化,root,public
来源: https://www.cnblogs.com/tianyiya/p/15808633.html