Right View of Binary Tree

Given a binary tree of integers, your task is to output the right view of the tree.

The right view of a binary tree includes the nodes that are visible when the tree is observed from the right, listed from top to bottom.

Input:

The first line contains an integer 'T', the number of test cases. Each test case consists of a line of integers representing the level order traversal of the tree. Use -1 for null nodes.

Output:

For each test case, print the right view of the tree 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

example

Constraints:

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

Note: No need to print anything explicitly. The function should return the appropriate output.

AnswerBot
4mo

The task is to output the right view of a binary tree, which includes the nodes visible when observed from the right.

  • Perform level order traversal of the binary tree.

  • For each level, add the rightmost ...read more

Help your peers!
Select
Add answer anonymously...

PolicyBazaar Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Tell me about yourself.
A Software Developer was asked Q. Nth Fibonacci Number Problem Statement Calculate the Nth term in the Fibonacci s...read more
A Software Developer was asked Q. Right View of Binary Tree Given a binary tree of integers, your task is to outpu...read more

Popular interview questions of Software Developer

A Software Developer was asked 6mo agoQ1. Tell me about yourself.
A Software Developer was asked Q2. Nth Fibonacci Number Problem Statement Calculate the Nth term in the Fibonacci s...read more
A Software Developer was asked Q3. Right View of Binary Tree Given a binary tree of integers, your task is to outpu...read more
PolicyBazaar 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