Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Print Right View of a given binary tree

Objec­tive: In a Binary Tree, print right view of it

Input: A binary tree.

What is Right View of a binary Tree?

When just look at the tree from the right side , all the nodes you can see will be the right view of the tree.


Right View of a binary tree

Right View of a binary tree

Approach :

Method 1 :

  • Tra­verse the tree from right to left
  • Print the first node you encounter
  • Take two vari­ables , currentLevel=0 and nextLevel=1
  • As soon as you change level , change the cur­rentLevel = nextLevel
  • Print only when cur­rent level<nextLevel so this way you will print only the first element
  • For rest of the nodes on the the level cur­rentLevel and nextLevel are equal so it wont print

Method 2:

Do the Level order tra­ver­sal and print the last node value

Com­plete Code:


5 15 35 45

You may also like...