[Leetcode] 0297. Serialize and Deserialize Binary Tree

Explore diverse LeetCode solutions in Python, C++, JavaScript, SQL, and TypeScript. Ideal for interview prep, learning, and code practice in multiple programming languages. Github Repo Link

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.

 

Example 1:

Input: root = [1,2,3,null,null,4,5]
Output: [1,2,3,null,null,4,5]

Example 2:

Input: root = []
Output: []

 

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • -1000 <= Node.val <= 1000

內容目錄

Python

				
					# time complexity: O(n)
# space complexity: O(h)
from collections import deque
from typing import Optional




class Codec:
    def serialize(self, root: Optional[TreeNode]) -> str:
        if not root:
            return ''

        queue = deque([root])
        result = []
        while queue:
            node = queue.popleft()
            if node:
                result.append(str(node.val))
                queue.extend([node.left, node.right])
            else:
                result.append('None')
        
        return ','.join(result)

    def deserialize(self, data: str) -> Optional[TreeNode]:
        if not data:
            return None

        dataList = data.split(',')
        root = TreeNode(int(dataList[0]))
        queue = deque([root])

        i = 1
        while queue and i < len(dataList):
            node = queue.popleft()
            leftVal, rightVal = dataList[i], dataList[i+1] if i + 1 < len(dataList) else 'None'

            if leftVal != 'None':
                node.left = TreeNode(int(leftVal))
                queue.append(node.left)

            if rightVal != 'None':
                node.right = TreeNode(int(rightVal))
                queue.append(node.right)

            i += 2

        return root


# Your Codec object will be instantiated and called as such:
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(5)
root.left.left = TreeNode(3)
root.left.right = TreeNode(4)

ser = Codec()
deser = Codec()

tree = ser.serialize(root)
print(tree)  # '1,2,5,3,4,None,None,None,None,None,None,'

ans = deser.deserialize(tree)
print(ans.val)  # 1
print(ans.left.val)  # 2
print(ans.right.val)  # 5
print(ans.left.left.val)  # 3
print(ans.left.right.val)  # 4
				
			
zh_TW繁體中文