My accepted Python solution


  • 0
    S
    def levelOrder(self, root):
            queue = []
            output = []
            level = []
    
            if root:
                queue.append(root)
                queue.append(None)
            
            while queue:
                node = queue.pop(0)
                if node:
                    level.append(node.data)
                    if node.left:
                        queue.append(node.left)
                    if node.right:
                        queue.append(node.right)
                else:
                    output.append(level)
                    if queue:
                        queue.append(None)
                    level= []
            
            return output

Log in to reply
 

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