Problem · Tree
Binary Tree Zigzag Level Order Traversal
Learn this problemProblem statement
Given the root of a binary tree, return its values level by level in zigzag order.
Read the root level from left to right, the next level from right to left, and continue alternating directions for every later level.
Function
zigzagLevelOrder(root: TreeNode) → int[][]Examples
Example 1
root = [3,9,20,null,null,15,7]return = [[3],[20,9],[15,7]]The second level reverses direction, while the third level returns to left-to-right order.
Example 2
root = [1,2,3,4,null,null,5]return = [[1],[3,2],[4,5]]Missing children do not create output entries, and direction still alternates by level.
Constraints
- The tree is finite and acyclic.
- Every node value is a signed integer.