-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path449-serialize-and-deserialize-bst.py
72 lines (63 loc) · 2.22 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
72
# 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: Optional[TreeNode]) -> str:
"""Encodes a tree to a single string.
"""
def int_to_string(val):
byte_array = [(val >> (i * 8)) & 0xFF for i in range(4)][:: - 1]
char_array = [chr(byte) for byte in byte_array]
string = ''.join(char_array)
return string
def build(node):
if not node:
return ''
cur = int_to_string(node.val)
left = build(node.left)
right = build(node.right)
return cur + left + right
return build(root)
def deserialize(self, data: str) -> Optional[TreeNode]:
"""Decodes your encoded data to tree.
"""
def string_to_int(string):
res = []
val = 0
for i, c in enumerate(string):
val += ord(c) << (8 * (4 - 1 - (i % 4)))
if i % 4 == 3:
res.append(val)
val = 0
return res
vals = string_to_int(data)
idx = 0
def build(lower, upper):
nonlocal idx
if idx >= len(vals):
return None
if vals[idx] <= lower or vals[idx] >= upper:
return None
node = TreeNode(vals[idx])
idx += 1
node.left = build(lower, node.val)
node.right = build(node.val, upper)
return node
return build(float('-inf'), float('inf'))
# 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
# time O(n)
# space O(n)
# using tree and dfs (preorder and recursive) and turn tree to string and bst and bit manipulate
'''
1. tree is BST, so we can use comparsion of vals to avoid record null val as encoded string
2. turn every val into string (length: 4) by bit manipulation to make encoded string compact
'''