Asked inMcAfee,SDE-2

Binary Tree Level Order Traversal

Given a binary tree of integers, return the level order traversal of the tree in a single line for each test case.

Input:

The input consists of multiple test cases as described:

The first line is an integer 'T', representing the number of test cases. For each test case, one line contains the elements of the tree in level order, where ‘-1’ signifies a null node.

Output:

Print the nodes in level order traversal, separated by a single space, for each test case.

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:

The level order traversal visits nodes level by level from left to right. Therefore, the output for the binary tree is a single line of node values in their visited order.

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, and 'data' is the node value.

Time Limit: 1 second

Note:

The actual implementation does not require any printing. It is sufficient to implement the function logic and return the result.

AnswerBot
4mo

Return the level order traversal of a binary tree in a single line for each test case.

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

  • Print the nodes in level order traversal separated ...read more

Help your peers!
Select
Add answer anonymously...
McAfee SDE-2 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