-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path297.二叉树的序列化与反序列化.py
129 lines (119 loc) · 3.01 KB
/
297.二叉树的序列化与反序列化.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
# @lc app=leetcode.cn id=297 lang=python3
#
# [297] 二叉树的序列化与反序列化
#
# https://leetcode.cn/problems/serialize-and-deserialize-binary-tree/description/
#
# algorithms
# Hard (58.37%)
# Likes: 979
# Dislikes: 0
# Total Accepted: 184.8K
# Total Submissions: 316.5K
# Testcase Example: '[1,2,3,null,null,4,5]'
#
#
# 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。
#
# 请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 /
# 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。
#
# 提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode
# 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。
#
#
#
# 示例 1:
#
#
# 输入:root = [1,2,3,null,null,4,5]
# 输出:[1,2,3,null,null,4,5]
#
#
# 示例 2:
#
#
# 输入:root = []
# 输出:[]
#
#
# 示例 3:
#
#
# 输入:root = [1]
# 输出:[1]
#
#
# 示例 4:
#
#
# 输入:root = [1,2]
# 输出:[1,2]
#
#
#
#
# 提示:
#
#
# 树中结点数在范围 [0, 10^4] 内
# -1000
#
#
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import collections
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
# 1. BFS O(N) O(N)
if not root:
return '[]'
q = collections.deque([root])
res = []
while q:
node = q.popleft()
if node:
res.append(str(node.val))
q.append(node.left)
q.append(node.right)
else:
res.append('null')
return '['+','.join(res)+']'
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
if data == '[]':
return
vals = data[1:-1].split(',')
root = TreeNode(int(vals[0]))
i = 1
q = collections.deque([root])
while q:
node = q.popleft()
if vals[i] != 'null':
node.left = TreeNode(vals[i])
q.append(node.left)
i += 1
if vals[i] != 'null':
node.right = TreeNode(vals[i])
q.append(node.right)
i += 1
return root
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# ans = deser.deserialize(ser.serialize(root))
# @lc code=end