Given the root of a binary tree, return the zigzag level order traversal of its nodes' values.
The zigzag pattern alternates direction at each level:
Return the result as a list of lists, where each inner list contains the node values at that depth level in the appropriate zigzag order.
Each node has a val (integer value), a left child, and a right child. Children may be null.
The tree is given as a level-order array where null represents a missing node. For example, [3,9,20,null,null,15,7] represents:
3 → Level 0: [3] (left to right)
/ \
9 20 ← Level 1: [20, 9] (right to left)
/ \
15 7 → Level 2: [15, 7] (left to right)
root = [3, 9, 20, null, null, 15, 7][[3], [20, 9], [15, 7]]root = [1][[1]]root = [][]The number of nodes in the tree is in the range [0, 2000]-100 <= Node.val <= 100Expected time complexity: O(n)Run your code to see results
Use Cmd+Enter to run