标签搜索

目 录CONTENT

文章目录

【算法模板】序列化和反序列化二叉树

陈铭
2022-05-11 / 0 评论 / 0 点赞 / 179 阅读 / 581 字 / 正在检测是否收录...

例题:序列化和反序列化二叉搜索树

力扣449题为例:

序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建。

设计一个算法来序列化和反序列化 二叉搜索树 。 对序列化/反序列化算法的工作方式没有限制。 您只需确保二叉搜索树可以序列化为字符串,并且可以将该字符串反序列化为最初的二叉搜索树。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {
	// 这里用后序遍历为例
    public String serialize(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        postOrder(root, list);
        String str = list.toString();
        return str.substring(1, str.length() - 1);
    }

    public TreeNode deserialize(String data) {
        if (data.isEmpty()) {
            return null;
        }
        String[] arr = data.split(", ");
		// 后序遍历下,Deque的结构是 --> [(root的左子树), (root的右子树), root]
		// 如果是前序遍历:[root, (root的左子树), (root的右子树)]
		// 如果是中序遍历:[(root的左子树), root, (root的右子树)]
		// 所以,之所以不使用中序遍历,就是root节点不好找
        Deque<Integer> stack = new ArrayDeque<Integer>();
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            stack.push(Integer.parseInt(arr[i]));
        }
        return construct(Integer.MIN_VALUE, Integer.MAX_VALUE, stack);
    }

    private void postOrder(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        postOrder(root.left, list);
        postOrder(root.right, list);
        list.add(root.val);
    }

    private TreeNode construct(int lower, int upper, Deque<Integer> stack) {
        if (stack.isEmpty() || stack.peek() < lower || stack.peek() > upper) {
            return null;
        }
		// Deque当作栈使用
		// 后序遍历下,弹出栈顶顺序是: root --> 右子树 --> 左子树
        int val = stack.pop();
        TreeNode root = new TreeNode(val);
		// 所以是先root.right,再root.left
        root.right = construct(val, upper, stack);
        root.left = construct(lower, val, stack);
		// 当然,以前序遍历来做,Deque当作队列使用
		// 前序遍历下,出队顺序是: root --> 左子树 --> 右子树
		// 代码应该变成这样
		// root.left = construct(lower, val, stack);
		// root.right = construct(val, upper, stack);
        return root;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// String tree = ser.serialize(root);
// TreeNode ans = deser.deserialize(tree);
// return ans;
0

评论区