Beat 100% Python solution


  • 0
    A
     def zigzagLevelOrder(self, root):
        if not root:
            return []
        result = []
        level = [root]
        count = 0
        while level:
            if count % 2 == 0:
                result.append([x.val for x in level])
            else:
                result.append([x.val for x in level[::-1]])
            next_level = []
            for node in level:
                if node.left:
                    next_level.append(node.left)
                if node.right:
                    next_level.append(node.right)
            level = next_level
            count += 1
        return result

Log in to reply
 

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