Repository: TheAlgorithms/Python
Pull request: serialize deserialize binary tree
### Describe your change:

Fixes #9611 


* [x] Add an algorithm?
* [ ] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?

### Checklist:
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [x] This PR only changes one algorithm file.  To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [ ] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
* [ ] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a [closing keyword](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue): "Fixes #ISSUE-NUMBER".


Problem statement:
Serialize and Deserialize
### Feature description

Design an algorithm to serialize and deserialize a binary tree. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Hints from issue discussion:
@Arunsiva003 I would like to work on this enhancement. Can you assign this to me?
> @Arunsiva003 I would like to work on this enhancement. Can you assign this to me?

You don't have to wait for any approval. make a pull request and proceed!
i started working on this

Commit message: format files

Patch to review:
diff --git a/data_structures/binary_tree/serialize_deserialize_binary_tree.py b/data_structures/binary_tree/serialize_deserialize_binary_tree.py
new file mode 100644
index 000000000000..e092ecf2c0d0
--- /dev/null
+++ b/data_structures/binary_tree/serialize_deserialize_binary_tree.py
@@ -0,0 +1,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()


Code review comments:
diff_hunk: @@ -0,0 +1,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:
path: data_structures/binary_tree/serialize_deserialize_binary_tree.py
text: @user1:
Please provide type hint for the parameter: `left`

Please provide type hint for the parameter: `right`

diff_hunk: @@ -0,0 +1,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():
path: data_structures/binary_tree/serialize_deserialize_binary_tree.py
text: @user1:
As there is no test file in this pull request nor any test function or class in the file `data_structures/binary_tree/serialize_deserialize_binary_tree.py`, please provide doctest for the function `build_tree`

Please provide return type hint for the function: `build_tree`. **If the function does not return a value, please provide the type hint as:** `def function() -> None:`