Zig-Zag Level Order Traversal of Binary Tree

Given a binary tree, your task is to perform a zigzag level order traversal. This traversal starts from left to right on the first level, then switches to right to left on the next level, and continues to alternate in this manner.

Input:

The first line contains an integer 'T', representing the number of test cases. Each test case consists of a single line containing the elements in level order, where nodes are separated by a space. Use -1 to represent null nodes, which are not part of the tree.

Output:

For each test case, output a single line with zigzag level order traversal of the tree's nodes, separated by spaces.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
1 3 2 4 5 6 7
Explanation:

The provided tree has the following levels:

  • Level 1: nodes {1}
  • Level 2: nodes {3, 2}
  • Level 3: nodes {4, 5, 6}
  • Level 4: nodes {7}

The zigzag traversal adheres to the pattern of navigating from left to right, then right to left for alternating levels.

Constraints:

  • 1 ≤ T ≤ 100
  • 0 ≤ N ≤ 3000
  • 0 ≤ val ≤ 105

Note: The input ends when all nodes at the last level are null (-1). You do not need to handle printing within your function.

AnswerBot
4mo

Perform zigzag level order traversal on a binary tree.

  • Use a queue to perform level order traversal of the binary tree.

  • Alternate between left to right and right to left traversal for each level.

  • Handle ...read more

Help your peers!
Select
Add answer anonymously...

Paytm Money Backend Developer interview questions & answers

A Backend Developer was asked Q. Given multiple trains traveling between multiple stations, write an SQL query to...read more
A Backend Developer was asked Q. Given a directed graph, determine if there is a cycle in the graph.
A Backend Developer was asked Q. Given a binary tree, return the zigzag level order traversal of its nodes' value...read more

Popular interview questions of Backend Developer

A Backend Developer was asked Q1. Given multiple trains traveling between multiple stations, write an SQL query to...read more
A Backend Developer was asked Q2. Given a directed graph, determine if there is a cycle in the graph.
A Backend Developer was asked Q3. Given a binary tree, return the zigzag level order traversal of its nodes' value...read more
Paytm Money Backend Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits