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.

Be the first one to answer
Add answer anonymously...
Juniper Networks Staff Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter