Update appNew update is available. Click here to update.

Right View

Contributed by
Ekansh Saxena
Last Updated: 23 Feb, 2023
Medium
yellow-spark
0/80
Avg time to solve 35 mins
Success Rate 65 %
Share
28 upvotes

Problem Statement

You have been given a Binary Tree of integers.

Your task is to print the Right view of it.

The right view of a Binary Tree is a set of nodes visible when the tree is viewed from the Right side and the nodes are printed from top to bottom order.

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 100
1 <= N <= 10^5
-10^9 <= data <= 10^9

Where 'N' is the number of nodes in the tree and 'data' is the value of a node in the given tree.

Time Limit: 1 sec
Sample Input 1 :
1
2 35 10 2 3 5 2 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output 1 :
2 10 2
Explanation of The Sample Input 1:

Sample Input 1

The right view of the tree contains all the extreme-right elements in each level of the tree, including the head of the tree.
Sample Input 2 :
1
1 2 -1 3 -1 4 -1 5 -1 -1 -1
Sample Output 2 :
1 2 3 4 5
Explanation of The Sample Input 2 :

Sample Input 2

Reset Code
Full screen
Auto
copy-code
Console