资讯专栏INFORMATION COLUMN

leetcode297. Serialize and Deserialize Binary Tree

desdik / 2042人阅读

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

题目要求
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.

设计一个方法将一个二叉树序列化并反序列化。序列化是指将对象转化成一个字符串,该字符串可以在网络上传输,并且到达目的地后可以通过反序列化恢复成原来的对象。

在Leetcode中,树的序列化是通过广度优先遍历实现的,就如题目中所介绍的那样。

这里我们使用中序遍历来解决这个问题。

思路和代码

中序遍历是指递归的先遍历当前节点,然后按同样的方式递归的遍历左子树,再递归的遍历右子树。因此题目中的例子的中序遍历结果为[1,2,3,4,5]。但是,标准的中序遍历并不能使我们将该结果反构造成一棵树,我们丢失了父节点和子节点之间的关系。因此我们需要适当的插入空值来保存父节点和子节点的关系。

因此,我们将访问到空值也记录下来,结果如下[1,2, , ,3,4, , ,5, , ]

这里我们也可以明显的看出来,中序遍历需要保存的空值远远多于广度优先遍历。

那么我们如何将这个字符串反序列化呢?
其实思路还是一样的,将其根据分隔符分割后传入队列,不断从队列头读取数据,先构建当前节点,然后依次构建左子树和右子树,如果遇到空值,则返回递归。

代码如下:

    private static final String NULL = "N";
    private static final String SPLITOR = ",";
    
     // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder result = new StringBuilder();
        inOrder(root, result);
        return result.toString();
    }
    
    private void inOrder(TreeNode root, StringBuilder result){
        if(root==null){
            result.append(NULL).append(SPLITOR);
        }else{
            result.append(root.val).append(SPLITOR);
            inOrder(root.left, result);
            inOrder(root.right, result);
        }
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        Deque nodes = new LinkedList(Arrays.asList(data.split(SPLITOR)));
        return buildTree(nodes);
    }
    
    private TreeNode buildTree(Deque nodes) {
        String val = nodes.remove();
        if (val.equals(NULL)) return null;
        else {
            TreeNode node = new TreeNode(Integer.valueOf(val));
            node.left = buildTree(nodes);
            node.right = buildTree(nodes);
            return node;
        }
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

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

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

相关文章

  • [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
  • LeetCode 297. Serialize and Deserialize Binary Tre

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

    cc17 评论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
  • Serialize and Deserialize Binary Tree & BST

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

    eccozhou 评论0 收藏0
  • [LintCode/LeetCode] Binary Tree Serialization

    摘要:这里要注意的是的用法。所以记住,用可以从自动分离出数组。跳过第一个元素并放入数组最快捷语句建立的用意记录处理过的结点并按处理所有结点和自己的连接下面先通过判断,再修改的符号的顺序,十分巧妙更轻便的解法 Problem Design an algorithm and write code to serialize and deserialize a binary tree. Writin...

    keithyau 评论0 收藏0

发表评论

0条评论

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