Simple python solution

  • 0
    class Solution:
        # @param {TreeNode} root
        # @return {integer[][]}
        def zigzagLevelOrder(self, root):
            if not root:
                return []
            prev, curr, result, flag = [root, ], [root, ], [], -1
            while curr:
                flag *= -1
                result.append([node.val for node in curr[::flag]])
                curr = []
                while prev:
                    node = prev.pop(0)
                    if node.left:
                    if node.right:
                prev = curr
            return result

Log in to reply

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