Python O( N ) solution. easy to understand
EDIT: Thanks to @WKVictor , this solution uses 'deque' instead of 'list' as queue. And the performance is O( N )\n\n\nclass Codec:\n\n    def serialize(self, ro
EDIT: Thanks to @WKVictor , this solution uses 'deque' instead of 'list' as queue. And the performance is O( N )\n\n```\nclass Codec:\n\n    def serialize(self, root):\n        vals = []\n\n        def preOrder(node):\n            if node:\n                vals.append(node.val)\n                preOrder(node.left)\n                preOrder(node.right)\n\n        preOrder(root)\n\n        return ' '.join(map(str, vals))\n\n    # O( N ) since each val run build once\n    def deserialize(self, data):\n        vals = collections.deque(int(val) for val in data.split())\n\n        def build(minVal, maxVal):\n            if vals and minVal < vals[0] < maxVal:\n                val = vals.popleft()\n                node = TreeNode(val)\n                node.left = build(minVal, val)\n                node.right = build(val, maxVal)\n                return node\n\n        return build(float('-infinity'), float('infinity'))\n\n```
[]