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.
Examples:
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]
Approach :
- 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:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
Node( int item)
{
data = item;
left = right = NULL;
}
};
struct _Max_level
{
int _max_level;
};
Node *root;
_Max_level *_max = new _Max_level();
void bottomRightViewUtil(Node* node, int level,
_Max_level *_max_level)
{
if (node == NULL)
return ;
bottomRightViewUtil(node->right,
level, _max_level);
if (_max_level->_max_level < level)
{
cout << node->data << " " ;
_max_level->_max_level = level;
}
bottomRightViewUtil(node->left,
level + 1, _max_level);
}
void bottomRightView(Node *node)
{
bottomRightViewUtil(node, 1, _max);
}
void bottomRightView()
{
bottomRightView(root);
}
int main()
{
root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->left->right = new Node(6);
bottomRightView();
}
|
Java
class Node {
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
class Max_level {
int max_level;
}
class BinaryTree {
Node root;
Max_level max = new Max_level();
void bottomRightViewUtil(Node node, int level,
Max_level max_level)
{
if (node == null )
return ;
bottomRightViewUtil(node.right,
level, max_level);
if (max_level.max_level < level) {
System.out.print(node.data + " " );
max_level.max_level = level;
}
bottomRightViewUtil(node.left,
level + 1 , max_level);
}
void bottomRightView()
{
bottomRightView(root);
}
void bottomRightView(Node node)
{
bottomRightViewUtil(node, 1 , max);
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 1 );
tree.root.left = new Node( 2 );
tree.root.right = new Node( 3 );
tree.root.left.left = new Node( 4 );
tree.root.right.left = new Node( 5 );
tree.root.right.left.right = new Node( 6 );
tree.bottomRightView();
}
}
|
Python3
class Node:
def __init__( self , item):
self .data = item
self .left = None
self .right = None
maxlevel = [ 0 ]
def bottomRightViewUtil(node, level, maxlevel):
if (node = = None ):
return
bottomRightViewUtil(node.right, level,
maxlevel)
if (maxlevel[ 0 ] < level):
print (node.data, end = " " )
maxlevel[ 0 ] = level
bottomRightViewUtil(node.left, level + 1 ,
maxlevel)
def bottomRightView(node):
bottomRightViewUtil(node, 1 , maxlevel)
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.right.left = Node( 5 )
root.right.left.right = Node( 6 )
bottomRightView(root)
|
C#
using System;
class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class Max_level
{
public int max_level;
}
class GFG
{
Node root;
Max_level max = new Max_level();
void bottomRightViewUtil(Node node, int level,
Max_level max_level)
{
if (node == null )
return ;
bottomRightViewUtil(node.right,
level, max_level);
if (max_level.max_level < level)
{
Console.Write(node.data + " " );
max_level.max_level = level;
}
bottomRightViewUtil(node.left,
level + 1, max_level);
}
void bottomRightView()
{
bottomRightView(root);
}
void bottomRightView(Node node)
{
bottomRightViewUtil(node, 1, max);
}
public static void Main(String []args)
{
GFG tree = new GFG();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.right.left = new Node(5);
tree.root.right.left.right = new Node(6);
tree.bottomRightView();
}
}
|
Javascript
<script>
class Node
{
constructor(item)
{
this .data = item;
this .left = null ;
this .right = null ;
}
}
class Max_level
{
constructor()
{
this .max_level = 0;
}
}
var max = new Max_level();
function bottomRightViewUtil(node, level,max_level)
{
if (node == null )
return ;
bottomRightViewUtil(node.right,
level, max_level);
if (max_level.max_level < level)
{
document.write(node.data + " " );
max_level.max_level = level;
}
bottomRightViewUtil(node.left,
level + 1, max_level);
}
function bottomRightView(node)
{
bottomRightViewUtil(node, 1, max);
}
var root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.left.right = new Node(6);
bottomRightView(root);
</script>
|
Time Complexity : O(N), where N is the number of nodes of the binary tree.
Auxiliary space: O(n) for implicit call stack as using recursion.
Iterative Approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int val)
{
Node* temp = new Node();
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
void bottomRightView(Node* root)
{
if (!root)
return ;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
while (size--) {
root = q.front();
q.pop();
while (root->right) {
if (root->left)
q.push(root->left);
root = root->right;
}
if (root->left)
q.push(root->left);
}
cout << root->data << " " ;
}
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->right->left = newNode(5);
root->right->left->right = newNode(6);
bottomRightView(root);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static class Node {
int data;
Node left, right;
Node( int data)
{
this .data = data;
this .left = this .right = null ;
}
}
static Node newNode( int val)
{
Node temp = new Node(val);
return temp;
}
static void bottomRightView(Node root)
{
if (root == null ) {
return ;
}
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int size = q.size();
while (size-- > 0 ) {
root = q.poll();
while (root.right != null ) {
if (root.left != null ) {
q.add(root.left);
}
root = root.right;
}
if (root.left != null ) {
q.add(root.left);
}
}
System.out.print(root.data + " " );
}
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.right.left = newNode( 5 );
root.right.left.right = newNode( 6 );
bottomRightView(root);
}
}
|
Python3
class Node:
def __init__( self , val):
self .data = val
self .left = None
self .right = None
def bottomRightView(root):
if not root:
return
q = []
q.append(root)
while q:
size = len (q)
while size > 0 :
root = q.pop( 0 )
while root.right:
if root.left:
q.append(root.left)
root = root.right
if root.left:
q.append(root.left)
size - = 1
print (root.data, end = " " )
def newNode(val):
temp = Node(val)
return temp
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.right.left = newNode( 5 )
root.right.left.right = newNode( 6 )
bottomRightView(root)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
public class Gfg{
public class Node {
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
static void bottomRightView(Node root)
{
if (root== null )
return ;
Queue<Node> q= new Queue<Node>();
q.Enqueue(root);
while (q.Count != 0) {
int size = q.Count;
while (size-->0) {
root = q.Peek();
q.Dequeue();
while (root.right != null ) {
if (root.left != null )
q.Enqueue(root.left);
root = root.right;
}
if (root.left != null )
q.Enqueue(root.left);
}
Console.Write(root.data+ " " );
}
}
public static void Main( string [] args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.left.right = new Node(6);
bottomRightView(root);
}
}
|
Javascript
<script>
class Node
{
constructor(item)
{
this .data = item;
this .left = null ;
this .right = null ;
}
}
function bottomRightView(root){
if (root == null )
return ;
var queue = [];
queue.push(root);
while (queue.length) {
var size = queue.length;
while (size--){
root = queue.shift();
while (root.right != null ){
if (root.left != null )
queue.push(root.left);
root = root.right;
}
if (root.left != null )
queue.push(root.left);
}
document.write(root.data + " " );
}
}
var root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.left.right = new Node(6);
bottomRightView(root);
</script>
|
Time Complexity: O(N), where N is the total nodes in a BT.
Auxiliary Space: O(n), where n is the maximum nodes possible in a diagonal.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!