Skip to content

Commit 5923c92

Browse files
committed
format files
1 parent cef886c commit 5923c92

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
class TreeNode:
22
"""
33
A binary tree node has a value, left child, and right child.
4-
4+
55
Props:
66
val(int): The value of the node.
77
left: The left child of the node.
88
right: The right child of the node.
99
"""
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:
1112
if not isinstance(val, int):
1213
raise TypeError("Value must be an integer.")
1314
self.val = val
1415
self.left = left
1516
self.right = right
1617

18+
1719
# Helper functions
1820
def are_trees_identical(root1: TreeNode, root2: TreeNode) -> bool:
1921
"""
2022
Check if two binary trees are identical.
21-
23+
2224
Args:
2325
root1 (TreeNode): Tree 1
2426
root2 (TreeNode): Tree 2
2527
2628
Returns:
2729
bool: True if the trees are identical, False otherwise.
28-
30+
2931
>>> root1 = TreeNode(1)
3032
>>> root1.left = TreeNode(2)
3133
>>> root1.right = TreeNode(3)
@@ -43,29 +45,30 @@ def are_trees_identical(root1: TreeNode, root2: TreeNode) -> bool:
4345
>>> are_trees_identical(root1, root2)
4446
False
4547
"""
46-
48+
4749
if not root1 and not root2:
4850
return True
4951
if not root1 or not root2:
5052
return False
51-
53+
5254
return (
5355
root1.val == root2.val
5456
and are_trees_identical(root1.left, root2.left)
5557
and are_trees_identical(root1.right, root2.right)
5658
)
5759

60+
5861
# Main functions
5962
def serialize(root: TreeNode) -> str:
6063
"""
6164
Serialize a binary tree to a string using preorder traversal.
62-
65+
6366
Args:
6467
root(TreeNode): The root of the binary tree.
65-
68+
6669
Returns:
6770
A string representation of the binary tree.
68-
71+
6972
>>> root = TreeNode(1)
7073
>>> root.left = TreeNode(2)
7174
>>> root.right = TreeNode(3)
@@ -77,23 +80,24 @@ def serialize(root: TreeNode) -> str:
7780
>>> serialize(root)
7881
'1,null,null'
7982
"""
80-
83+
8184
# Use "null" to represent empty nodes in the serialization
8285
if not root:
83-
return "null"
84-
86+
return "null"
87+
8588
return str(root.val) + "," + serialize(root.left) + "," + serialize(root.right)
8689

90+
8791
def deserialize(data: str) -> TreeNode:
8892
"""
8993
Deserialize a string to a binary tree.
90-
91-
Args:
94+
95+
Args:
9296
data(str): The serialized string.
93-
97+
9498
Returns:
9599
The root of the binary tree.
96-
100+
97101
>>> root = TreeNode(1)
98102
>>> root.left = TreeNode(2)
99103
>>> root.right = TreeNode(3)
@@ -110,26 +114,27 @@ def deserialize(data: str) -> TreeNode:
110114
>>> are_trees_identical(root, deserialized)
111115
False
112116
"""
113-
117+
114118
# Split the serialized string by comma to get node values
115-
nodes = data.split(",")
116-
119+
nodes = data.split(",")
120+
117121
def build_tree():
118122
# Get the next value from the list
119-
val = nodes.pop(0)
120-
123+
val = nodes.pop(0)
124+
121125
if val == "null":
122126
return None
123-
127+
124128
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+
128132
return node
129133

130134
return build_tree()
131-
135+
136+
132137
if __name__ == "__main__":
133138
import doctest
134139

135-
doctest.testmod()
140+
doctest.testmod()

0 commit comments

Comments
 (0)