We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 24c5f6f commit 1edb34eCopy full SHA for 1edb34e
serialize_deserialize_narrytree.py
@@ -0,0 +1,36 @@
1
+class Node:
2
+ def __init__(self, val):
3
+ self.val = val
4
+ self.children = []
5
+
6
+class Codec:
7
+ def serialize(self, root):
8
+ if root is None:
9
+ return ""
10
+ res = [root.val, "#"]
11
+ q = collections.deque([root])
12
+ while q:
13
+ node = q.popleft()
14
+ for child in node.children:
15
+ res.append(child.val)
16
+ q.append(child)
17
+ res.append("#")
18
+ return ",".join(res)
19
20
+ def deserialize(self, s):
21
+ if len(s) == 0:
22
+ return
23
+ vals = s.split(",")
24
+ q = collections.deque()
25
+ root = Node(vals[0])
26
+ q.append(root)
27
+ i = 1
28
29
30
+ i += 1
31
+ while vals[i] != "#":
32
+ child = Node(vals[i])
33
+ node.children.append(child)
34
35
36
+ return root
0 commit comments