Python iterative solution


  • 0
    D
    from collections import deque
    
    class Codec:
    
        def serialize(self, root):
            """Encodes a tree to a single string.
            
            :type root: TreeNode
            :rtype: str
            """
            queue = deque([root])
            res = []
            while queue:
                node = queue.popleft()
                res.append(str(node.val) if node else '#')
                if node:
                    queue += node.left, node.right
            return ','.join(res)
            
            
    
        def deserialize(self, data):
            """Decodes your encoded data to tree.
            
            :type data: str
            :rtype: TreeNode
            """
            nodes = [TreeNode(v) if v != '#' else None for v in data.split(',')]
            if not nodes:
                return None
            nodesIter = iter(nodes)
            queue = deque([next(nodesIter)])
            while queue:
                node = queue.popleft()
                if not node:
                    continue
                node.left = next(nodesIter)
                node.right = next(nodesIter)
                if node:
                    queue += node.left, node.right
            return nodes[0]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.