Level Order Traversal of Binary Tree

Given a Binary Tree of integers, your task is to return the level order traversal of the tree.

Input:

The first line contains an integer 'T' which denotes the number of test cases. For each test case, the first line contains elements of the tree in the level order form. The elements are space-separated values of the nodes, where '-1' represents a null node.

Output:

For each test case, output the nodes in level order traversal separated by a single space, with each test case output on a new line.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
1 2 3 4 5 6 7
Explanation:
Level 1:
The root node is 1

Level 2:
Left child of 1 is 2
Right child of 1 is 3

Level 3:
Left child of 2 is 4
Right child of 2 is null (-1)
Left child of 3 is 5
Right child of 3 is 6

Level 4:
Left child of 4 is null (-1)
Right child of 4 is 7
All children of 5 and 6 are null (-1)

Constraints:

  • 1 <= T <= 100
  • 0 <= N <= 1000
  • 0 <= data <= 106 and data != -1

Where ‘T’ is the number of test cases, ‘N’ is the total number of nodes in the binary tree, and ‘data’ is the value of the binary tree node.

AnswerBot
4mo

Return the level order traversal of a Binary Tree of integers.

  • Perform a level order traversal using a queue data structure

  • Start by pushing the root node into the queue

  • Pop each node from the queue, pri...read more

Help your peers!
Select
Add answer anonymously...

Juniper Networks Staff Engineer interview questions & answers

A Staff Engineer was asked Q. Why is security important in a system?
A Staff Engineer was asked Q. What are the differences between TCP and UDP?
A Staff Engineer was asked Q. Implement a telephone directory program.

Popular interview questions of Staff Engineer

A Staff Engineer was asked Q1. Why is security important in a system?
A Staff Engineer was asked Q2. What are the differences between TCP and UDP?
A Staff Engineer was asked Q3. Implement a telephone directory program.
Juniper Networks Staff 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