FastPrepBinary Tree Zigzag Level Order Traversal
Problems/Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal

Microsoft logoMicrosoft● MediumFULLTIMEINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Microsoft onsite interview. Our version is about 99% the same as the reported question.

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

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

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

Interview reports that mention this question

  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
All Microsoft interview questionsSee Microsoft 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 List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        // write your code here
    }
}
Checking account…