美文网首页
二叉树序列化与反序列化

二叉树序列化与反序列化

作者: shiguangfeixu | 来源:发表于2021-03-15 23:39 被阅读0次

Algorithm

449. Serialize and Deserialize BST

Description

Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You need to ensure that a binary search tree can be serialized to a string, and this string can be deserialized to the original tree structure.

The encoded string should be as compact as possible.

Example 1:

Input: root = [2,1,3]
Output: [2,1,3]

Example 2:

Input: root = []
Output: []

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • 0 <= Node.val <= 104
  • The input tree is guaranteed to be a binary search tree.

Solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {

    private int index = 0;

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder res = new StringBuilder();
        dfs(root, res);
        return res.toString();
    }

    private void dfs(TreeNode root, StringBuilder res){
        if(root == null){
            res.append("null").append(' ');
            return;
        }
        res.append(root.val).append(' ');
        dfs(root.left, res);
        dfs(root.right, res);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        String[] arr = data.split(" ");
        index = 0;
        return build(arr);
    }

    public TreeNode build(String[] arr) {
        if(arr[index].equals("null")) {
            index++;
            return null;
        }
        
        TreeNode root = new TreeNode(Integer.parseInt(arr[index]));
        index++;
        root.left = build(arr);
        root.right = build(arr);

        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;

相关文章

  • 二叉树序列化和反序列化

    二叉树序列化和反序列化 前序 序列化和反序列化

  • 297. Serialize and Deserialize B

    二叉树序列化与反序列化 Runtime: 172 ms, faster than 42.05% Memory Us...

  • 二叉树的三种遍历方法

    二叉树的序列化 为了方便构造二叉树来验证我们的算法,这里先介绍下二叉树的序列化和反序列化。 序列化 先序遍历整颗二...

  • LeetCode:序列化二叉树

    面试题37. 序列化二叉树 请实现两个函数,分别用来序列化和反序列化二叉树。示例:你可以将以下二叉树: 序列化为 ...

  • 剑指offer刷题记录(C++版本)(之七)

    61.序列化二叉树??? 题目:请实现两个函数,分别用来序列化和反序列化二叉树 二叉树的序列化是指:把一棵二叉树按...

  • JZ-061-序列化二叉树

    序列化二叉树 题目描述 请实现两个函数,分别用来序列化和反序列化二叉树。二叉树的序列化是指:把一棵二叉树按照某种遍...

  • 二叉树的序列化 和反序列化

    对于二叉树的序列化与反序列化有n种方法,本文以先序和层序的方式序列化和反序列化 对于,以先序的方式序列化: 1、以...

  • 面试题37:序列化二叉树

    题目 实现两个函数,分别用来序列化和反序列化二叉树 解题思路 序列化根据前序遍历的顺序序列化二叉树,从根节点开始,...

  • 剑指Offer-61 二叉树序列化

    请实现两个函数,分别用来序列化和反序列化二叉树 利用广度遍历实现二叉树的序列化和非序列化。核心思想:广度遍历

  • LeetCode 297. 二叉树的序列化与反序列化 | Pyt

    297. 二叉树的序列化与反序列化 题目来源:力扣(LeetCode)https://leetcode-cn.co...

网友评论

      本文标题:二叉树序列化与反序列化

      本文链接:https://www.haomeiwen.com/subject/tafrcltx.html