42ms 94.48% Python solution, easy to understand


  • 0
    K
    class Solution(object):
        def zigzagLevelOrder(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if not root: return []
            reverse = False
            level = [root]
            res = []
            while level:
                if reverse: res.append([node.val for node in level[::-1]])
                else: res.append([node.val for node in level])
                pairs = [(node.left, node.right) for node in level]
                level = [node for pair in pairs for node in pair if node]
                reverse = not reverse
            return res
    

Log in to reply
 

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