Binary Tree Right View Problem Statement

Given a binary tree of integers, your task is to print the right view of it. The right view represents a set of nodes visible when the tree is viewed from the right side. These nodes are printed in a top-to-bottom order.

Input:

The first line contains an integer 'T' which denotes the number of test cases. Then, the test cases follow.
The only line of input for each test case contains the elements of the tree in level order form, separated by a single space.
If a node does not have a left or right child, use -1 in its place.
All nodes of the last level will be null (-1).

Output:

For each test case, print the right view of the tree. Output for each test case should be on a new line.

Example:

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

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 105
  • -109 <= data <= 109

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

Be the first one to answer
Add answer anonymously...
Ansys Software Private Limited Software Developer 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