Most straight forward Python solution using two queue


  • 0
    Q
    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)
                levelVals.append(node.val)
                if not queue:
                    result.append(levelVals)
                    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.