Skip to content

Latest commit

 

History

History
130 lines (76 loc) · 3.06 KB

0297._Serialize_and_Deserialize_Binary_Tree.md

File metadata and controls

130 lines (76 loc) · 3.06 KB

297. Serialize and Deserialize Binary Tree

难度: Hard

刷题内容

原题连接

内容描述

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.

Example: 

You may serialize the following tree:

    1
   / \
  2   3
     / \
    4   5

as "[1,2,3,null,null,4,5]"
Clarification: The above 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.

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

解题方案

思路 1 - 时间复杂度: O(N)- 空间复杂度: O(N)******

简单是简单,但是也折腾了一会, beats 44.01%

class Codec:

    def serialize(self, root):
        """Encodes a tree to a single string.
        
        :type root: TreeNode
        :rtype: str
        """
        nodes, res = [], []
        def dfs(node, level, nodes):
            if level > len(nodes):
                nodes.append([])
            if not node:
                nodes[level-1].append(None)
                return
            else:
                nodes[level-1].append(node.val)
            dfs(node.left, level+1, nodes)
            dfs(node.right, level+1, nodes)
        dfs(root, 1, nodes)
        for lst in nodes[:-1]:
            res += lst
        return res
            
    def deserialize(self, data):
        """Decodes your encoded data to tree.
        
        :type data: str
        :rtype: TreeNode
        """
        if not data:
            return None
        
        root = TreeNode(data[0])
        nodes = collections.deque()
        for i in range(1, len(data)):
            if data[i] is None:
                node = None
            else:
                node = TreeNode(data[i])
            nodes.append(node)
            
        stack = [root] # 一次只搞一层
        while nodes:
            new_stack = []
            for node in stack:
                if node:
                    new_left_node, new_right_node = nodes.popleft(), nodes.popleft()
                    if new_left_node:
                        node.left = new_left_node
                    if new_right_node:
                        node.right = new_right_node
                    new_stack.append(new_left_node)
                    new_stack.append(new_right_node) 
            stack = new_stack
        return root