Most straight forward Python solution using two queue

  • 0
    def levelOrder(self, root):
            if not root: return []
            queue, queue2, result, levelVals = [root], [], [], []
            while queue:
                node = queue.pop(0)
                if node.left: queue2.append(node.left)
                if node.right: queue2.append(node.right)
                if not queue:
                    levelVals = []
                    queue, queue2 = queue2, queue
            return result

Log in to reply

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