Print Right View of a given binary tree
Objective: 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.
Method 1 :
- Traverse the tree from right to left
- Print the first node you encounter
- Take two variables , currentLevel=0 and nextLevel=1
- As soon as you change level , change the currentLevel = nextLevel
- Print only when current level<nextLevel so this way you will print only the first element
- For rest of the nodes on the the level currentLevel and nextLevel are equal so it wont print
Do the Level order traversal and print the last node value
5 15 35 45
Top Companies Interview Questions..-
If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.