1
+ class TreeNode :
2
+ """
3
+ A binary tree node has a value, left child, and right child.
4
+
5
+ Props:
6
+ val(int): The value of the node.
7
+ left: The left child of the node.
8
+ right: The right child of the node.
9
+ """
10
+ def __init__ (self , val :int = 0 , left = None , right = None ) -> None :
11
+ if not isinstance (val , int ):
12
+ raise TypeError ("Value must be an integer." )
13
+ self .val = val
14
+ self .left = left
15
+ self .right = right
16
+
17
+ # Helper functions
18
+ def are_trees_identical (root1 : TreeNode , root2 : TreeNode ) -> bool :
19
+ """
20
+ Check if two binary trees are identical.
21
+
22
+ Args:
23
+ root1 (TreeNode): Tree 1
24
+ root2 (TreeNode): Tree 2
25
+
26
+ Returns:
27
+ bool: True if the trees are identical, False otherwise.
28
+
29
+ >>> root1 = TreeNode(1)
30
+ >>> root1.left = TreeNode(2)
31
+ >>> root1.right = TreeNode(3)
32
+ >>> root2 = TreeNode(1)
33
+ >>> root2.left = TreeNode(2)
34
+ >>> root2.right = TreeNode(3)
35
+ >>> are_trees_identical(root1, root2)
36
+ True
37
+ >>> root1 = TreeNode(1)
38
+ >>> root1.left = TreeNode(2)
39
+ >>> root1.right = TreeNode(3)
40
+ >>> root2 = TreeNode(1)
41
+ >>> root2.left = TreeNode(2)
42
+ >>> root2.right = TreeNode(4)
43
+ >>> are_trees_identical(root1, root2)
44
+ False
45
+ """
46
+
47
+ if not root1 and not root2 :
48
+ return True
49
+ if not root1 or not root2 :
50
+ return False
51
+
52
+ return (
53
+ root1 .val == root2 .val
54
+ and are_trees_identical (root1 .left , root2 .left )
55
+ and are_trees_identical (root1 .right , root2 .right )
56
+ )
57
+
58
+ # Main functions
59
+ def serialize (root : TreeNode ) -> str :
60
+ """
61
+ Serialize a binary tree to a string using preorder traversal.
62
+
63
+ Args:
64
+ root(TreeNode): The root of the binary tree.
65
+
66
+ Returns:
67
+ A string representation of the binary tree.
68
+
69
+ >>> root = TreeNode(1)
70
+ >>> root.left = TreeNode(2)
71
+ >>> root.right = TreeNode(3)
72
+ >>> root.right.left = TreeNode(4)
73
+ >>> root.right.right = TreeNode(5)
74
+ >>> serialize(root)
75
+ '1,2,null,null,3,4,null,null,5,null,null'
76
+ >>> root = TreeNode(1)
77
+ >>> serialize(root)
78
+ '1,null,null'
79
+ """
80
+
81
+ # Use "null" to represent empty nodes in the serialization
82
+ if not root :
83
+ return "null"
84
+
85
+ return str (root .val ) + "," + serialize (root .left ) + "," + serialize (root .right )
86
+
87
+ def deserialize (data : str ) -> TreeNode :
88
+ """
89
+ Deserialize a string to a binary tree.
90
+
91
+ Args:
92
+ data(str): The serialized string.
93
+
94
+ Returns:
95
+ The root of the binary tree.
96
+
97
+ >>> root = TreeNode(1)
98
+ >>> root.left = TreeNode(2)
99
+ >>> root.right = TreeNode(3)
100
+ >>> root.right.left = TreeNode(4)
101
+ >>> root.right.right = TreeNode(5)
102
+ >>> serialzed_data = serialize(root)
103
+ >>> deserialized = deserialize(serialzed_data)
104
+ >>> are_trees_identical(root, deserialized)
105
+ True
106
+ >>> root = TreeNode(1)
107
+ >>> serialzed_data = serialize(root)
108
+ >>> dummy_data = "1,2,null,null,3,4,null,null,5,null,null"
109
+ >>> deserialized = deserialize(dummy_data)
110
+ >>> are_trees_identical(root, deserialized)
111
+ False
112
+ """
113
+
114
+ # Split the serialized string by comma to get node values
115
+ nodes = data .split ("," )
116
+
117
+ def build_tree ():
118
+ # Get the next value from the list
119
+ val = nodes .pop (0 )
120
+
121
+ if val == "null" :
122
+ return None
123
+
124
+ node = TreeNode (int (val ))
125
+ node .left = build_tree () # Recursively build left subtree
126
+ node .right = build_tree () # Recursively build right subtree
127
+
128
+ return node
129
+
130
+ return build_tree ()
131
+
132
+ if __name__ == "__main__" :
133
+ import doctest
134
+
135
+ doctest .testmod ()
0 commit comments