Be the first user to complete this post

  • 0
Add to List
Medium

63. Print Right View of Binary Tree

Objective: In a Binary Tree, print the right view of it

What is the Right View of a Binary Tree?

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

Example:

Right View of a binary tree

Approach :

  • The idea is to traverse the tree from right to left and print the first node you encounter at each level.
  • To implement the above
    • Take two variables, currentLevel = 0 and nextLevel=0
    • Every time you go left or right, nextLevel by 1. 
    • If current level<nextLevel, print the node and set currentLevel = nextLevel. This way you will print only the first node you encounter at each level

Note: Another approach would be to Do the Level order traversal from left to right and print the last node value

 
5 15 35 45