Given a Binary Tree, print Right view of it. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side.
Input : 10 / \ 2 3 / \ / \ 7 8 12 15 / 14 Output : 10 3 15 14 The output nodes are the rightmost nodes of their respective levels.
If we observe carefully, we will see that our main task is to print the right most node of every level. So, we will do a level order traversal on the tree and print the rightmost node at every level.
Below is the implementation of above approach:
10 3 15 14
Time Complexity: O( n ), where n is the number of nodes in the binary tree.
This article is contributed by Bibhas Abhishek. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Print Right View of a Binary Tree
- Print Bottom-Right View of a Binary Tree
- Sum of nodes in the right view of the given binary tree
- Convert left-right representation of a binary tree to down-right
- Bottom View of a Binary Tree using Recursion
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Print Nodes in Top View of Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Iterative Method To Print Left View of a Binary Tree
- Sum of nodes in top view of binary tree
- Sum of nodes in the left view of the given binary tree
- Sum of nodes in bottom view of Binary Tree
- Print nodes in top view of Binary Tree | Set 2
- Print Left View of a Binary Tree
- Bottom View of a Binary Tree
- Modify a binary tree to get preorder traversal using right pointers only
- Print leaf nodes in binary tree from left to right using one stack