-
Notifications
You must be signed in to change notification settings - Fork 43
/
serialize-and-deserialize-binary-tree.py
581 lines (522 loc) · 17.5 KB
/
serialize-and-deserialize-binary-tree.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
"""
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.
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: []
Example 3:
Input: root = [1]
Output: [1]
Example 4:
Input: root = [1,2]
Output: [1,2]
Constraints:
The number of nodes in the tree is in the range [0, 104].
-1000 <= Node.val <= 1000
"""
# V0
# IDRA : DFS
class Codec:
def serialize(self, root):
""" Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
def rserialize(root, string):
""" a recursive helper function for the serialize() function."""
# check base case
if root is None:
string += 'None,'
else:
string += str(root.val) + ','
string = rserialize(root.left, string)
string = rserialize(root.right, string)
return string
return rserialize(root, '')
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
def rdeserialize(l):
""" a recursive helper function for deserialization."""
if l[0] == 'None':
l.pop(0)
return None
root = TreeNode(l[0])
l.pop(0)
root.left = rdeserialize(l)
root.right = rdeserialize(l)
return root
data_list = data.split(',')
root = rdeserialize(data_list)
return root
# V0
# IDEA : DFS + tree property + recursive + queue
class Codec:
### DFS
def serialize(self, root):
vals = []
def preOrder(root):
if not root:
vals.append('#')
### NOTE : we have else logic, and put all other cases under it
# -> TODO : check if we can do `if elif...else`
else:
vals.append(str(root.val))
preOrder(root.left)
preOrder(root.right)
preOrder(root)
return ' '.join(vals)
def deserialize(self, data):
vals = [val for val in data.split()]
### NOTE : recursive
def build():
### NOTE : when there is element in vals, we keep recursive running
if vals:
### NOTE : vals already retrieved via `[val for val in data.split()]`
# -> so every time we pop its 1st element, we are able to get all if elements one by one
# -> then we can build the tree via recursive (root.left = build(), root.right = build())
val = vals.pop(0)
if val == '#':
return None
### NOTE : we get root via current val (val = vals.popleft())
root = TreeNode(int(val))
### NOTE : root.left comes from build()
root.left = build()
### NOTE : root.right comes from build()
root.right = build()
### NOTE : we need to return root
return root
return build()
# V0'
# IDEA : DFS + tree property + recursive + collections.deque
import collections
class Codec:
### DFS
def serialize(self, root):
vals = []
def preOrder(root):
if not root:
vals.append('#')
### NOTE : we have else logic, and put all other cases under it
# -> TODO : check if we can do `if elif...else`
else:
vals.append(str(root.val))
preOrder(root.left)
preOrder(root.right)
preOrder(root)
return ' '.join(vals)
def deserialize(self, data):
vals = collections.deque(val for val in data.split())
### NOTE : recursive
def build():
### NOTE : when there is element in vals, we keep recursive running
if vals:
### NOTE : we use popleft
val = vals.popleft()
if val == '#':
return None
### NOTE : we get root via current val (val = vals.popleft())
root = TreeNode(int(val))
### NOTE : root.left comes from build()
root.left = build()
### NOTE : root.right comes from build()
root.right = build()
### NOTE : we need to return root
return root
return build()
# V1
# IDEA : DFS
# CHECK Solution description !!!
# https://leetcode.com/problems/serialize-and-deserialize-binary-tree/solution/
# Deserialization
class Codec:
def serialize(self, root):
""" Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
def rserialize(root, string):
""" a recursive helper function for the serialize() function."""
# check base case
if root is None:
string += 'None,'
else:
string += str(root.val) + ','
string = rserialize(root.left, string)
string = rserialize(root.right, string)
return string
return rserialize(root, '')
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
def rdeserialize(l):
""" a recursive helper function for deserialization."""
if l[0] == 'None':
l.pop(0)
return None
root = TreeNode(l[0])
l.pop(0)
root.left = rdeserialize(l)
root.right = rdeserialize(l)
return root
data_list = data.split(',')
root = rdeserialize(data_list)
return root
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79571892
# IDEA : DEQUE (collections.deque)
# Deque DEMO
# -> A double-ended queue, or deque, supports adding and removing elements from either end. The more commonly used stacks and queues are degenerate forms of deques, where the inputs and outputs are restricted to a single end.
# https://pymotw.com/2/collections/deque.html
#
# In [30]: import collections
# ...:
# ...: d = collections.deque('abcdefg')
# ...: print (d)
# ...: print (len(d))
# ...: print ('left end :', d[0])
# ...: print ('right end :', d[-1])
# ...: print ('pop :' , d.pop())
# ...: print (d)
# ...: print ('pop_left:' , d.popleft())
# ...: print (d)
# ...:
# deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
# 7
# left end : a
# right end : g
# pop : g
# deque(['a', 'b', 'c', 'd', 'e', 'f'])
# pop_left: a
# deque(['b', 'c', 'd', 'e', 'f'])
import collections
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
vals = []
def preOrder(root):
if not root:
vals.append('#')
else:
vals.append(str(root.val))
preOrder(root.left)
preOrder(root.right)
preOrder(root)
return ' '.join(vals)
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
vals = collections.deque(val for val in data.split())
def build():
if vals:
val = vals.popleft()
if val == '#':
return None
root = TreeNode(int(val))
root.left = build()
root.right = build()
return root
return build()
### Test case : dev
# V1
# IDEA : ASCII
# http://zxi.mytechroad.com/blog/tree/leetcode-297-serialize-and-deserialize-binary-tree/
# C++
# class Codec {
# public:
#
# // Encodes a tree to a single string.
# string serialize(TreeNode* root) {
# ostringstream out;
# serialize(root, out);
# return out.str();
# }
#
# // Decodes your encoded data to tree.
# TreeNode* deserialize(string data) {
# istringstream in(data);
# return deserialize(in);
# }
# private:
# void serialize(TreeNode* root, ostringstream& out) {
# if (!root) {
# out << "# ";
# return;
# }
# out << root->val << " ";
# serialize(root->left, out);
# serialize(root->right, out);
# }
#
# TreeNode* deserialize(istringstream& in) {
# string val;
# in >> val;
# if (val == "#") return nullptr;
# TreeNode* root = new TreeNode(stoi(val));
# root->left = deserialize(in);
# root->right = deserialize(in);
# return root;
# }
# };
# V1
# IDEA : BINARY
# http://zxi.mytechroad.com/blog/tree/leetcode-297-serialize-and-deserialize-binary-tree/
# C++
# class Codec {
# public:
#
# // Encodes a tree to a single string.
# string serialize(TreeNode* root) {
# ostringstream out;
# serialize(root, out);
# return out.str();
# }
#
# // Decodes your encoded data to tree.
# TreeNode* deserialize(string data) {
# istringstream in(data);
# return deserialize(in);
# }
# private:
# enum STATUS {
# ROOT_NULL = 0x0,
# ROOT = 0x1,
# LEFT = 0x2,
# RIGHT = 0x4
# };
#
# void serialize(TreeNode* root, ostringstream& out) {
# char status = 0;
# if (root) status |= ROOT;
# if (root && root->left) status |= LEFT;
# if (root && root->right) status |= RIGHT;
# out.write(&status, sizeof(char));
# if (!root) return;
# out.write(reinterpret_cast<char*>(&(root->val)), sizeof(root->val));
# if (root->left) serialize(root->left, out);
# if (root->right) serialize(root->right, out);
# }
#
# TreeNode* deserialize(istringstream& in) {
# char status;
# in.read(&status, sizeof(char));
# if (!status & ROOT) return nullptr;
# auto root = new TreeNode(0);
# in.read(reinterpret_cast<char*>(&root->val), sizeof(root->val));
# root->left = (status & LEFT) ? deserialize(in) : nullptr;
# root->right = (status & RIGHT) ? deserialize(in) : nullptr;
# return root;
# }
# };
# V1'
# http://bookshadow.com/weblog/2015/10/26/leetcode-serialize-and-deserialize-binary-tree/
# IDEA : BINARY TREE TRANVERSE
class Codec:
def serialize(self, root):
def doit(node):
if node:
vals.append(str(node.val))
doit(node.left)
doit(node.right)
else:
vals.append('#')
vals = []
doit(root)
return ' '.join(vals)
def deserialize(self, data):
def doit():
val = next(vals)
if val == '#':
return None
node = TreeNode(int(val))
node.left = doit()
node.right = doit()
return node
vals = iter(data.split())
return doit()
# V1''
# http://bookshadow.com/weblog/2015/10/26/leetcode-serialize-and-deserialize-binary-tree/
# IDEA : BINARY TREE TRANVERSE (BY LAYER)
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
if not root:
return '[]'
res = [root.val]
q = collections.deque([root])
while q:
front = q.popleft()
if front.left:
q.append(front.left)
if front.right:
q.append(front.right)
res.append(front.left.val if front.left else 'null')
res.append(front.right.val if front.right else 'null')
while res and res[-1] == 'null':
res.pop()
return '[' + ','.join(map(str, res)) + ']'
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
if data == '[]':
return None
nodes = collections.deque([[TreeNode(o), None][o == 'null']
for o in data[1:-1].split(',')])
q = collections.deque([nodes.popleft()]) if nodes else None
root = q[0] if q else None
while q:
parent = q.popleft()
left = nodes.popleft() if nodes else None
right = nodes.popleft() if nodes else None
parent.left, parent.right = left, right
if left:
q.append(left)
if right:
q.append(right)
return root
# V1'''
# http://bookshadow.com/weblog/2015/10/26/leetcode-serialize-and-deserialize-binary-tree/
# IDEA : JSON + DICT
import json
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
if not root:
return 'null'
nodes = collections.deque([root])
maps = collections.deque([{'v' : root.val}])
tree = maps[0]
while nodes:
frontNode = nodes.popleft()
frontMap = maps.popleft()
if frontNode.left:
frontMap['l'] = {'v' : frontNode.left.val}
nodes.append(frontNode.left)
maps.append(frontMap['l'])
if frontNode.right:
frontMap['r'] = {'v' : frontNode.right.val}
nodes.append(frontNode.right)
maps.append(frontMap['r'])
return json.dumps(tree)
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
tree = json.loads(data)
if not tree:
return None
root = TreeNode(tree['v'])
maps = collections.deque([tree])
nodes = collections.deque([root])
while nodes:
frontNode = nodes.popleft()
frontMap = maps.popleft()
left, right = frontMap.get('l'), frontMap.get('r')
if left:
frontNode.left = TreeNode(left['v'])
maps.append(left)
nodes.append(frontNode.left)
if right:
frontNode.right = TreeNode(right['v'])
maps.append(right)
nodes.append(frontNode.right)
return root
# V1''''
# http://bookshadow.com/weblog/2015/10/26/leetcode-serialize-and-deserialize-binary-tree/
# IDEA : JSON + TUPLE
import json
class Codec:
def serialize(self, root):
def tuplify(root):
return root and (root.val, tuplify(root.left), tuplify(root.right))
return json.dumps(tuplify(root))
def deserialize(self, data):
def detuplify(t):
if t:
root = TreeNode(t[0])
root.left = detuplify(t[1])
root.right = detuplify(t[2])
return root
return detuplify(json.loads(data))
# V1'''''
# https://leetcode.com/problems/serialize-and-deserialize-binary-tree/discuss/74259/Recursive-preorder-Python-and-C%2B%2B-O(n)
class Codec:
def serialize(self, root):
def doit(node):
if node:
vals.append(str(node.val))
doit(node.left)
doit(node.right)
else:
vals.append('#')
vals = []
doit(root)
return ' '.join(vals)
def deserialize(self, data):
def doit():
val = next(vals)
if val == '#':
return None
node = TreeNode(int(val))
node.left = doit()
node.right = doit()
return node
vals = iter(data.split())
return doit()
# V1''''''
# IDEA : BFS
# https://leetcode.com/problems/serialize-and-deserialize-binary-tree/discuss/166904/Python-or-BFS-tm
class Codec:
def serialize(self, root):
if not root: return ""
q = collections.deque([root])
res = []
while q:
node = q.popleft()
if node:
q.append(node.left)
q.append(node.right)
res.append(str(node.val) if node else '#')
return ','.join(res)
def deserialize (self, data):
if not data: return None
nodes = data.split(',')
root = TreeNode(int(nodes[0]))
q = collections.deque([root])
index = 1
while q:
node = q.popleft()
if nodes[index] is not '#':
node.left = TreeNode(int(nodes[index]))
q.append(node.left)
index += 1
if nodes[index] is not '#':
node.right = TreeNode(int(nodes[index]))
q.append(node.right)
index += 1
return root
# V2