-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0449-serialize-and-deserialize-bst.py
More file actions
35 lines (26 loc) · 1 KB
/
0449-serialize-and-deserialize-bst.py
File metadata and controls
35 lines (26 loc) · 1 KB
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
# time complexity: O(n)
# space complexity: O(n)
from typing import Optional
class Codec:
def serialize(self, root: Optional[TreeNode]) -> str:
def postorder(root):
return postorder(root.left) + postorder(root.right) + [root.val] if root else []
return ' '.join(map(str, postorder(root)))
def deserialize(self, data: str) -> Optional[TreeNode]:
def helper(lower=float('-inf'), upper=float('inf')):
if not data or data[-1] < lower or data[-1] > upper:
return None
val = data.pop()
root = TreeNode(val)
root.right = helper(val, upper)
root.left = helper(lower, val)
return root
data = [int(x) for x in data.split(' ') if x]
return helper()
# 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