MCPcopy
hub / github.com/HuberTRoy/leetCode / serialize

Method serialize

Tree/SerializeAndDeserializeBinaryTree.py:131–166  ·  view source on GitHub ↗

Encodes a tree to a single string. :type root: TreeNode :rtype: str

(self, root)

Source from the content-addressed store, hash-verified

129class Codec:
130
131 def serialize(self, root):
132 """Encodes a tree to a single string.
133
134 :type root: TreeNode
135 :rtype: str
136 """
137 if not root:
138 return None
139
140 result = []
141
142 def _serialize(roots):
143 _next = []
144 for i in roots:
145 if i:
146 result.append(i.val)
147 _next.append(i.left)
148 _next.append(i.right)
149
150 else:
151 result.append(None)
152
153 return _next
154
155 base = _serialize([root])
156
157 while any(base):
158 base = _serialize(base)
159
160 while 1:
161 if result[-1] == None:
162 result.pop()
163 else:
164 break
165
166 return str(result)
167
168
169 def deserialize(self, data):

Callers

nothing calls this directly

Calls 1

popMethod · 0.45

Tested by

no test coverage detected