Leetcode 297. Serialize and Deserialize Binary Tree

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 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.

Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Algorithm

Use dfs to serialize and deserialize the tree.

Code

# 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
        """
        def dfs(node):
            if not node:
                ans.append("#")
                return
            ans.append(str(node.val))
            dfs(node.left)
            dfs(node.right)
        
        ans = []
        dfs(root)
        return ",".join(ans)

    def deserialize(self, data):
        """Decodes your encoded data to tree.
        
        :type data: str
        :rtype: TreeNode
        """
        def dfs():
            nonlocal index
            index = index + 1
            val = vals[index]
            if val == "#":
                return None
            node = TreeNode(int(val))
            node.left = dfs()
            node.right = dfs()
            return node
        
        vals = data.split(",")
        index = -1
        return dfs()
        

# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# ans = deser.deserialize(ser.serialize(root))

相关推荐

  1. Leetcode--27

    2024-06-06 01:58:04       56 阅读
  2. LeetCode290. Word Pattern

    2024-06-06 01:58:04       56 阅读
  3. [leetcode] 290. 单词规律

    2024-06-06 01:58:04       40 阅读
  4. leetcode 207.课程表

    2024-06-06 01:58:04       29 阅读
  5. leetcode290:单词规律

    2024-06-06 01:58:04       40 阅读
  6. LeetCode 290. 单词规律

    2024-06-06 01:58:04       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 01:58:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 01:58:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 01:58:04       87 阅读
  4. Python语言-面向对象

    2024-06-06 01:58:04       96 阅读

热门阅读

  1. 视觉SLAM

    2024-06-06 01:58:04       27 阅读
  2. 003 Spring注解

    2024-06-06 01:58:04       18 阅读
  3. nuxt3 api如何透传(不引第3方库)

    2024-06-06 01:58:04       28 阅读
  4. Lisp解析器技术文档

    2024-06-06 01:58:04       19 阅读
  5. Django 默认 CSRF 保护机制

    2024-06-06 01:58:04       34 阅读
  6. C语言编译与链接

    2024-06-06 01:58:04       31 阅读
  7. 设计模式(简要,应付软考)

    2024-06-06 01:58:04       25 阅读
  8. 概率图模型在自然语言处理中的应用

    2024-06-06 01:58:04       25 阅读
  9. AWS与SAP扩大战略合作:通过AI增强ERP解决方案

    2024-06-06 01:58:04       31 阅读
  10. 6月01日,每日信息差

    2024-06-06 01:58:04       28 阅读