Python3 Solution Explained (video + code) (inorder + preorder)
\nhttps://www.youtube.com/watch?v=g6Q1cpQvz8A\n\nclass Codec:\n\n    def serialize(self, root: TreeNode) -> str:\n        """Encodes a tree to a single string.\
[](https://www.youtube.com/watch?v=g6Q1cpQvz8A)\nhttps://www.youtube.com/watch?v=g6Q1cpQvz8A\n```\nclass Codec:\n\n    def serialize(self, root: TreeNode) -> str:\n        """Encodes a tree to a single string.\n        """\n        res = []\n        \n        def preorder(root):\n            if root:\n                res.append(str(root.val))\n                preorder(root.left)\n                preorder(root.right)\n        \n        preorder(root)\n        return " ".join(res)\n\n    def deserialize(self, data: str) -> TreeNode:\n        """Decodes your encoded data to tree.\n        """\n        data = [int(x) for x in data.split()]\n        \n        def build(pre, inorder):\n            if not pre:\n                return None\n            \n            node = TreeNode(pre[0])\n            temp = inorder.index(node.val)\n            node.left = build(pre[1: temp + 1], inorder[:temp])\n            node.right = build(pre[temp + 1 :], inorder[temp + 1 :])\n            \n            return node\n        \n        return build(data, sorted(data))\n```
['Python3']