资讯专栏INFORMATION COLUMN

Serialize and Deserialize Binary Tree & BST

eccozhou / 3141人阅读

摘要:思路理论上说所有遍历的方法都可以。但是为了使和的过程都尽量最简单,是不错的选择。用作为分隔符,来表示。复杂度代码思路这道题和之前不同,一般的树变成了,而且要求是。还是可以用,还是需要分隔符,但是就不需要保存了。

297. Serialize and Deserialize Binary Tree

Serialization is the process of 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 tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
For example, you may serialize the following tree

    1    
   /   
  2   3
     / 
    4   5

as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

思路

理论上说所有遍历的方法都可以。但是为了使serialize和deserialize的过程都尽量最简单,preorder是不错的选择。serialize的话,dfs比较好写,deserialize的话preorder和bfs比较好写。用“,”作为分隔符,“#”来表示null。例子里serialize之后结果就变成"1,2,3,#,#,4,5"。deserialize的时候用一个queue来保存string。

复杂度

Time: O(N), Space: O(N)

代码
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        // base case
        if(root == null) return "";
        StringBuilder encoded = new StringBuilder();
        encode(root, encoded);
        return encoded.substring(1).toString();
    }
    
    private void encode(TreeNode root, StringBuilder sb) {
        if(root == null) {
            sb.append(",#");
            return;
        }
        sb.append(",").append(root.val);
        encode(root.left, sb);
        encode(root.right, sb);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        // base case
        if(data.length() == 0) return null;
        Queue q = new LinkedList(Arrays.asList(data.split(",")));
        return decode(q);
    }
    
    private TreeNode decode(Queue q) {
        if(q.isEmpty()) return null;
        String cur = q.poll();
        if(cur.equals("#")) return null;
        TreeNode root = new TreeNode(Integer.valueOf(cur));
        root.left = decode(q);
        root.right = decode(q);
        return root;
    }
449. Serialize and Deserialize BST

Serialization is the process of 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.

The encoded string should be as compact as possible.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

思路

这道题和之前不同,一般的树变成了BST,而且要求是as compact as possible。还是可以用preorder,还是需要分隔符,但是null就不需要保存了。deserialize部分要变得复杂,left的值总是小于root的值,right的值总是大于root的值,根据这个每次recursion的时候把左边的值都放到另一个queue里面,剩下的就是右边的值。

复杂度

Time: O(N^2), Space: O(N)

代码
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        // base case
        if(root == null) return "";
        StringBuilder encoded = new StringBuilder();
        encode(root, encoded);
        return encoded.substring(1).toString();
    }
    
    private void encode(TreeNode root, StringBuilder sb) {
        if(root == null) return;
        sb.append(",").append(root.val);
        encode(root.left, sb);
        encode(root.right, sb);
    }
    
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        // base case
        if(data.length() == 0) return null;
        Queue q = new LinkedList();
        for(String s : data.split(",")) q.offer(Integer.valueOf(s));
        return decode(q);
    }
    
    private TreeNode decode(Queue q) {
        if(q.isEmpty()) return null;
        int cur = q.poll();
        TreeNode root = new TreeNode(cur);
        Queue left = new LinkedList(); 
        while(!q.isEmpty() && q.peek() < cur) left.offer(q.poll());
        root.left = decode(left);
        root.right = decode(q);
        return root;
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66478.html

相关文章

  • leetcode449. Serialize and Deserialize BST

    摘要:题目要求将二叉搜索树序列化和反序列化,序列化是指将树用字符串的形式表示,反序列化是指将字符串形式的树还原成原来的样子。假如二叉搜索树的节点较多,该算法将会占用大量的额外空间。 题目要求 Serialization is the process of converting a data structure or object into a sequence of bits so that...

    Honwhy 评论0 收藏0
  • 297. Serialize and Deserialize Binary Tree

    摘要:题目解答用一个特殊的符号来表示的情况这是按先序遍历来存到中去这里用为其包含了几乎所有这里还是挺多知识点的,后是一个可以把数组变成则是把加到这个的中去 题目:Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stor...

    focusj 评论0 收藏0
  • [LeetCode] 297. Serialize and Deserialize Binary T

    Problem Serialization is the process of 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 li...

    MRZYD 评论0 收藏0
  • leetcode297. Serialize and Deserialize Binary Tree

    摘要:因此题目中的例子的中序遍历结果为。但是,标准的中序遍历并不能使我们将该结果反构造成一棵树,我们丢失了父节点和子节点之间的关系。这里我们也可以明显的看出来,中序遍历需要保存的空值远远多于广度优先遍历。 题目要求 Serialization is the process of converting a data structure or object into a sequence of ...

    desdik 评论0 收藏0
  • LeetCode 297. Serialize and Deserialize Binary Tre

    摘要:题目大意将二叉树序列化,返回序列化的,和反序列化还原。解题思路技巧在于将记录为便于将来判断。的思想将每一层记录下来,反序列化时也按照层级遍历的方法依次设置为上一个里面的元素的左孩子和右孩子。变种,可以要求输出一个,而不是 LeetCode 297. Serialize and Deserialize Binary Tree 题目大意: 将二叉树序列化,返回序列化的String,和反序列...

    cc17 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<