forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_deserialize_binary_tree.py
140 lines (110 loc) · 3.5 KB
/
serialize_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
class TreeNode:
"""
A binary tree node has a value, left child, and right child.
Props:
val(int): The value of the node.
left: The left child of the node.
right: The right child of the node.
"""
def __init__(self, val: int = 0, left=None, right=None) -> None:
if not isinstance(val, int):
raise TypeError("Value must be an integer.")
self.val = val
self.left = left
self.right = right
# Helper functions
def are_trees_identical(root1: TreeNode, root2: TreeNode) -> bool:
"""
Check if two binary trees are identical.
Args:
root1 (TreeNode): Tree 1
root2 (TreeNode): Tree 2
Returns:
bool: True if the trees are identical, False otherwise.
>>> root1 = TreeNode(1)
>>> root1.left = TreeNode(2)
>>> root1.right = TreeNode(3)
>>> root2 = TreeNode(1)
>>> root2.left = TreeNode(2)
>>> root2.right = TreeNode(3)
>>> are_trees_identical(root1, root2)
True
>>> root1 = TreeNode(1)
>>> root1.left = TreeNode(2)
>>> root1.right = TreeNode(3)
>>> root2 = TreeNode(1)
>>> root2.left = TreeNode(2)
>>> root2.right = TreeNode(4)
>>> are_trees_identical(root1, root2)
False
"""
if not root1 and not root2:
return True
if not root1 or not root2:
return False
return (
root1.val == root2.val
and are_trees_identical(root1.left, root2.left)
and are_trees_identical(root1.right, root2.right)
)
# Main functions
def serialize(root: TreeNode) -> str:
"""
Serialize a binary tree to a string using preorder traversal.
Args:
root(TreeNode): The root of the binary tree.
Returns:
A string representation of the binary tree.
>>> root = TreeNode(1)
>>> root.left = TreeNode(2)
>>> root.right = TreeNode(3)
>>> root.right.left = TreeNode(4)
>>> root.right.right = TreeNode(5)
>>> serialize(root)
'1,2,null,null,3,4,null,null,5,null,null'
>>> root = TreeNode(1)
>>> serialize(root)
'1,null,null'
"""
# Use "null" to represent empty nodes in the serialization
if not root:
return "null"
return str(root.val) + "," + serialize(root.left) + "," + serialize(root.right)
def deserialize(data: str) -> TreeNode:
"""
Deserialize a string to a binary tree.
Args:
data(str): The serialized string.
Returns:
The root of the binary tree.
>>> root = TreeNode(1)
>>> root.left = TreeNode(2)
>>> root.right = TreeNode(3)
>>> root.right.left = TreeNode(4)
>>> root.right.right = TreeNode(5)
>>> serialzed_data = serialize(root)
>>> deserialized = deserialize(serialzed_data)
>>> are_trees_identical(root, deserialized)
True
>>> root = TreeNode(1)
>>> serialzed_data = serialize(root)
>>> dummy_data = "1,2,null,null,3,4,null,null,5,null,null"
>>> deserialized = deserialize(dummy_data)
>>> are_trees_identical(root, deserialized)
False
"""
# Split the serialized string by comma to get node values
nodes = data.split(",")
def build_tree():
# Get the next value from the list
val = nodes.pop(0)
if val == "null":
return None
node = TreeNode(int(val))
node.left = build_tree() # Recursively build left subtree
node.right = build_tree() # Recursively build right subtree
return node
return build_tree()
if __name__ == "__main__":
import doctest
doctest.testmod()