To solve this problem, you'll have to open it on the computer

Zig-Zag Level Order Traversal

Tree
medium
Score: 40

Given the root of a binary tree, return the zig-zag level-order traversal of its node’s values (i.e, from left to right, then right to left for next level and alternate between).

Class TreeNode:
    val (int)
    left (TreeNode)
    right (TreeNode)

Input Format

First Parameter - TreeNode root

Output Format

Return the 2D array.

Example 1:

img

Input:
    root = [3 9 20 null null 15 7]
Output: 
    [[3] [20 9] [15 7]]

Example 2:

Input:
    root = [10]
Output:
    [[10]]

Constraints:

  • The number of nodes in the tree is in the range [1, 2000].
  • -100 <= Node.val <= 100
  • Expected Time Complexity: O(N)
  • Expected Space Complexity: O(N)
Submit code to see the your result here