Binary Tree Zigzag Level Order Traversal
Updated
Problem 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) → List<List<Integer>>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.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Zigzag Level-Order TraversalFlipkart · ONSITE INTERVIEW
- Binary Tree Zigzag Level Order TraversalLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Top View of a Binary TreeCapillary Technologies · ONSITE INTERVIEWCapillary Technologies interview questions
- Binary Tree Level-Order Traversal by LevelsAdobe · PHONE SCREEN · ONSITE INTERVIEWAdobe interview questions
- Breadth-First Values of a Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
- Independent Level Iterators Over a Shared Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Group Identical Tree LevelsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Binary Tree Nodes at Distance KApple · ONSITE INTERVIEWApple interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions