-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path449. Serialize and Deserialize BST.py
71 lines (71 loc) · 1.96 KB
/
449. Serialize and Deserialize BST.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Codec:
def serialize(self, root: TreeNode) -> str:
"""Encodes a tree to a single string.
"""
l=[]
self.preorder(root,l)
print(l)
s=''
for i in l:
s+=str(i)+' '
print(s)
return s
def preorder(self,root,l):
if root:
l.append(root.val)
#l+=str(root.val)+' '
self.preorder(root.left,l)
self.preorder(root.right,l)
def deserialize(self, data: str) -> TreeNode:
"""Decodes your encoded data to tree.
"""
if len(data)==0:
return
l=[]
s=''
root=TreeNode(None)
for i in data:
if i!=' ':
s+=i
else:
l.append(s)
self.insert(root,int(s))
s=''
print(l)
return root
def insert(self,root,data):
if root.val is None:
root.val=data
return
if root.val>data:
if root.left:
self.insert(root.left,data)
else:
root.left=TreeNode(data)
else:
if root.right:
self.insert(root.right,data)
else:
root.right=TreeNode(data)
# Your Codec object will be instantiated and called as such:
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# tree = ser.serialize(root)
# ans = deser.deserialize(tree)
# return ans