Binary Tree Spiral Level Order
Updated
Problem statement
Return the values of a binary tree one level at a time in alternating directions.
For this exercise, assume the root level is traversed left to right, the next level right to left, and directions alternate thereafter. Group values by their distance from the root and omit missing children. Preserve node positions even when values repeat. An empty tree returns an empty list.
Function
zigzagLevels(root: TreeNode) → int[][]Examples
Example 1
root = [3,9,20,null,null,15,7]return = [[3],[20,9],[15,7]]The root is left-to-right, the second level is right-to-left, and the third returns to left-to-right.
Example 2
root = [1,2,3,4,null,null,5]return = [[1],[3,2],[4,5]]Null links are skipped; the second level is [3,2] and the third is [4,5].
Example 3
root = []return = []An empty tree has no levels.
Example 4
root = [1,2,3,4,5,6,7]return = [[1],[3,2],[4,5,6,7]]A full tree with three levels. Level 1 is read left to right: [1]. Level 2 is read right to left: [3,2]. Level 3 is read left to right again: [4,5,6,7].
Constraints
- For this exercise, assume a finite acyclic binary tree with
0through500nodes. -10^6 <= node.val <= 10^6.
Source note: Source images are source-neutral excerpts of author-reported interview prompts.
More Amazon Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
- Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
- Distance Between Two Nodes in a Binary TreeBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Time to Burn a Binary Tree From One NodeAdobe · PHONE SCREENAdobe interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions