FastPrepZigzag Level-Order Traversal
Problems/Zigzag Level-Order Traversal

Zigzag Level-Order Traversal

Flipkart logoFlipkart● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This exercise comes from a reported onsite problem-solving round. It asked for a zigzag traversal of a binary tree. We chose the function signature, what happens with a null tree, the limits, and the examples. Our version is about 91% the same as the reported question.

Return the values of a binary tree level by level in zigzag order.

Read the root level from left to right, the next level from right to left, and continue alternating direction at every level. Return an empty matrix when root is null.

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 traversal alternates left-to-right, right-to-left, then left-to-right.

Example 2

root = [1]return = [[1]]
root
1

A one-node tree has one level.

Example 3

root = []return = []
root
empty

An empty tree produces no levels.

Constraints

  • The tree contains between 0 and 2000 nodes.
  • -100000 <= node.val <= 100000

Source note: The source image is a source-neutral excerpt of the author-reported prompt.

More Flipkart interview questions

  • K-th Element of Two Sorted ArraysONSITE INTERVIEW · Seen Oct 2026
  • Minimized Total Idle TimeOA · Seen Dec 2025

Similar questions at other companies

  • Binary Tree Zigzag Level Order TraversalLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Binary Tree Zigzag Level Order TraversalMicrosoft · ONSITE INTERVIEWMicrosoft 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
See Flipkart 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…