Spiral Order Traversal of a Binary Tree

Given a binary tree with N nodes, your task is to output the Spiral Order traversal of the binary tree.

Input:

The input consists of a single line containing elements of the binary tree provided in level order. The node values are separated by a space, and -1 represents a null node.

Output:

A line of N space-separated integers representing the spiral order traversal of the binary tree.

Example:

Input: 1 2 3 -1 -1 4 5
Output: 1 3 2 4 5
Explanation:
The binary tree corresponding to the input is:
1
/ \
2 3
/ \
4 5
The Spiral Order traversal is: 1 3 2 4 5

Constraints:

  • 0 <= N <= 10^4
  • Where N is the total number of nodes in the binary tree.
  • Time Limit: 1 second
Note:
You are only required to implement the function that returns the spiral order traversal as a list. All input/output handling has been taken care of.
AnswerBot
4mo

Implement a function to return the spiral order traversal of a binary tree.

  • Traverse the binary tree in a spiral order, alternating between left to right and right to left.

  • Use a queue to keep track of ...read more

Help your peers!
Select
Add answer anonymously...

Bosch Global Software Technologies Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 8mo agoQ. Write a simple string-based coding question with implementation.
An Associate Software Engineer was asked 9mo agoQ. What is the difference between malloc and calloc?
An Associate Software Engineer was asked 9mo agoQ. Are you comfortable working in Bangalore?

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 8mo agoQ1. Write a simple string-based coding question with implementation.
An Associate Software Engineer was asked 10mo agoQ2. What is the difference between malloc and calloc?
An Associate Software Engineer was asked 10mo agoQ3. Are you comfortable working in Bangalore?
Bosch Global Software Technologies Associate Software Engineer 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