Given a Binary Tree, print Bottom-Right view of it. The Bottom Right view of a Binary Tree is a set of nodes visible when the tree is visited from Bottom Right side, return the values of the nodes ordered from right to left.
In the bottom-right view, on viewing the tree at an angle of 45 degrees from the bottom-right side and only a few nodes would be visible and rest would be hidden behind them.
Input : 1 / \ 2 3 \ \ 5 4 Output : [4, 5] Visible nodes from the bottom right direction are 4 and 5 Input : 1 / \ 2 3 / / 4 5 \ 6 Output: [3, 6, 4]
- The problem can be solved using simple recursive traversal.
- Keep track of the level of a node by passing a parameter to all recursive calls. Consider the level of a tree at an angle of 45% (as explained in an example), so whenever we move towards the left, its level would increase by one.
- The idea is to keep track of the maximum level and traverse the tree in a manner that right subtree is visited before left subtree.
- A node whose level is more than maximum level so far, Print the node because this is the last node in its level (Traverse the right subtree before left subtree).
Below is the implementation of the above approach:
3 6 4
Time Complexity : O(N), where N is the number of nodes of the binary tree.
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.
- Print Right View of a Binary Tree
- 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
- Print nodes in top view of Binary Tree | Set 2
- Print Left View of a Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Bottom View of a Binary Tree
- Sum of nodes in top view of binary tree
- Sum of nodes in the right view of the given binary tree
- Sum of nodes in the left view of the given binary tree
- Sum of nodes in bottom view of Binary Tree
- Right view of Binary Tree using Queue
- Bottom View of a Binary Tree using Recursion
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a Generic Tree(N-array Tree) to Binary Tree
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.