FastPrepBinary Tree Spiral Level Order
Problems/Binary Tree Spiral Level Order

Binary Tree Spiral Level Order

Amazon logoAmazon● MediumINTERNFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The Applied Scientist intern phone screen reports spiral-order traversal in a tree. It omits the tree arity, first direction, result grouping and size limits; this exercise supplies the conventional binary-tree zigzag contract. Approximate source match: 85%.

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]]
root
3920157

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]]
root
12435

Null links are skipped; the second level is [3,2] and the third is [4,5].

Example 3

root = []return = []
root
empty

An empty tree has no levels.

Example 4

root = [1,2,3,4,5,6,7]return = [[1],[3,2],[4,5,6,7]]
root
1245367

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 0 through 500 nodes.
  • -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

Interview reports that mention this question

  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II (L5) Interview Questions: 5 Experiences

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
See Amazon hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int[][] zigzagLevels(TreeNode root) {
        // Write your code here
    }
}
Checking account…