python AC 36ms


  • 0
    Y
    class Solution(object):
        def zigzagLevelOrder(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if not root:
                return []
            order = 1
            
            res = []
            queue = [root]
            
            while queue:
                tempQueue = []
                level = []
                for node in queue:
                    level.append(node.val)
                    if node.left:
                        tempQueue.append(node.left)
                    if node.right:
                        tempQueue.append(node.right)
                        
                res.append(level[::order])
                order *= -1
                queue = tempQueue
                
            return res
    

Log in to reply
 

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