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