Python solution using three lists


  • 1
    D
    def levelOrder(self, root):
            if not root:
                return []
            ans, currLevel, nextLevel = [], [root], []
            while currLevel:
                ans.append(list(x.val for x in currLevel))
                for node in currLevel:
                    if node.left:
                        nextLevel.append(node.left)
                    if node.right:
                        nextLevel.append(node.right)
                currLevel, nextLevel = nextLevel, []
            return ans

Log in to reply
 

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