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.
- Print Right View of a Binary Tree
- Print nodes in top view of Binary Tree | Set 2
- Print nodes in the Top View of Binary Tree | Set 3
- Print Left View of a Binary Tree
- Print Nodes in Top View of Binary Tree
- Iterative Method To Print Left View of a Binary Tree
- Bottom View of a Binary Tree
- Sum of nodes in top view of binary tree
- Right view of Binary Tree using Queue
- Sum of nodes in the right view of the given binary tree
- Sum of nodes in bottom view of Binary Tree
- Sum of nodes in the left view of the given binary tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print all k-sum paths in a binary tree
- Print Binary Tree in 2-Dimensions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.