zoukankan      html  css  js  c++  java
  • [Notes] 2020.6.16 每日一题 二叉树的序列化与反序列化

    题目

    序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。
    
    请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。
    
    示例: 
    
    你可以将以下二叉树:
    
        1
       / 
      2   3
         / 
        4   5
    
    序列化为 "[1,2,3,null,null,4,5]"
    提示: 这与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。
    
    说明: 不要使用类的成员 / 全局 / 静态变量来存储状态,你的序列化和反序列化算法应该是无状态的。
    
    通过次数33,138
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    

    结题思路

    序列化就是广度搜索一下而已,利用队列;
    反序列话比较麻烦一点,需要预定好树的结构然后往里面填数据。

    代码

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Codec:
    
        def serialize(self, root):
            """Encodes a tree to a single string.
            
            :type root: TreeNode
            :rtype: str
            """
            if not root:
                return '[]'
            queue = [root]
            serialize_list = []
            while len(queue) > 0:
                tmp_node = queue.pop(0)
                if tmp_node:
                    queue.append(tmp_node.left)
                    queue.append(tmp_node.right)
                    serialize_list.append(str(tmp_node.val))
                else:
                    serialize_list.append('null')
            return '[' + ','.join(serialize_list) + ']'
            
    
        def deserialize(self, data):
            """Decodes your encoded data to tree.
            
            :type data: str
            :rtype: TreeNode
            """
            import math
            data_queue = data[1:-1].split(',')
            total_lenght = len(data_queue)
            if total_lenght == 1:
                return None if len(data_queue[0])==0 else TreeNode(data_queue[0])
            n = 1+ math.floor(math.log(len(data_queue), 2))
            root = TreeNode(data_queue[0])
            parent_queue = [root]
            ind = 1
            for i in range(1, n):
                lenght = int(math.pow(2, i))
                tmp_len = 0
                while tmp_len < lenght and ind < total_lenght:
                    tmp_parent = parent_queue.pop(0)
                    # left
                    tmp1 = TreeNode(data_queue[ind])
                    tmp_parent.left = tmp1
                    parent_queue.append(tmp1)
                    ind += 1
                    tmp_len += 1
                    if ind == total_lenght:
                        break
                    # right
                    tmp2 = TreeNode(data_queue[ind])
                    tmp_parent.right = tmp2
                    parent_queue.append(tmp2)
                    ind += 1
                    tmp_len += 1
                    if ind == total_lenght:
                        break
            return root
            
    
    # Your Codec object will be instantiated and called as such:
    # codec = Codec()
    # codec.deserialize(codec.serialize(root))
    
  • 相关阅读:
    【Java】国内maven私服
    【密码学】常见证书格式及相互转换
    【密码学】数字证书原理
    【Java】Java与数字证书
    【Java】在eclipse中使用maven进行项目构建 入门篇
    【Java】在eclipse中使用gradle进行项目构建 入门篇
    Practice encryptedblobstore
    47、[源码]-Spring容器创建-初始化MessageSource
    46、[源码]-Spring容器创建-注册BeanPostProcessors
    45、[源码]-Spring容器创建-执行BeanFactoryPostProcessor
  • 原文地址:https://www.cnblogs.com/immortalBlog/p/13143816.html
Copyright © 2011-2022 走看看