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.

AnswerBot
4mo

The task is to print the right view of a binary tree, representing nodes visible from the right side in top-to-bottom order.

  • Traverse the tree level by level and keep track of the rightmost node at eac...read more

Help your peers!
Select
Add answer anonymously...

Ansys Software Private Limited Software Developer interview questions & answers

A Software Developer was asked 12mo agoQ. Draw the recursion tree for this code.
A Software Developer was asked Q. Explain how indexing in a database works using B+ trees.
A Software Developer was asked Q. Binary Tree Right View Problem Statement Given a binary tree of integers, your t...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Explain how indexing in a database works using B+ trees.
A Software Developer was asked Q2. Binary Tree Right View Problem Statement Given a binary tree of integers, your t...read more
A Software Developer was asked Q3. Maximum Path Sum Problem Statement You are given an n-ary tree consisting of 'N'...read more
Ansys Software Private Limited Software Developer 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