Print Bottom-Right View of a Binary Tree

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:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print bottom
// right view of binary tree
  
// A binary tree node
class Node {
  
    int data;
    Node left, right;
  
    Node(int item)
    {
        data = item;
        left = right = null;
    }
}
  
// class to access maximum level by reference
class Max_level {
  
    int max_level;
}
  
class BinaryTree {
  
    Node root;
    Max_level max = new Max_level();
  
    // Recursive function to print bottom
    // right view of a binary tree.
    void bottomRightViewUtil(Node node, int level,
                             Max_level max_level)
    {
  
        // Base Case
        if (node == null)
            return;
  
        // Recur for right subtree first
        bottomRightViewUtil(node.right,
                            level, max_level);
  
        // If this is the last Node of its level
        if (max_level.max_level < level) {
            System.out.print(node.data + " ");
            max_level.max_level = level;
        }
  
        // Recur for left subtree with increased level
        bottomRightViewUtil(node.left,
                            level + 1, max_level);
    }
  
    void bottomRightView()
    {
        bottomRightView(root);
    }
  
    // A wrapper over bottomRightViewUtil()
    void bottomRightView(Node node)
    {
  
        bottomRightViewUtil(node, 1, max);
    }
  
    // Driver program to test the above functions
    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();
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print bottom
# right view of binary tree
  
# A binary tree node
class Node: 
      
    # Construct to create a newNode 
    def __init__(self, item): 
        self.data = item
        self.left = None
        self.right = None
  
maxlevel = [0]
  
# Recursive function to print bottom
# right view of a binary tree.
def bottomRightViewUtil(node, level, maxlevel):
      
    # Base Case
    if (node == None):
        return
      
    # Recur for right subtree first
    bottomRightViewUtil(node.right, level,
                                 maxlevel)
      
    # If this is the last Node of its level
    if (maxlevel[0] < level):
        print(node.data, end = " ")
        maxlevel[0] = level
          
    # Recur for left subtree with increased level
    bottomRightViewUtil(node.left, level + 1,  
                                maxlevel)
  
# A wrapper over bottomRightViewUtil()
def bottomRightView(node):
      
    bottomRightViewUtil(node, 1, maxlevel)
          
# Driver Code
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)
  
# This code is contributed by SHUBHAMSINGH10

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print bottom
// right view of binary tree
using System;
  
// A binary tree node
class Node 
{
    public int data;
    public Node left, right;
  
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
  
// class to access maximum level by reference
class Max_level
{
    public int max_level;
}
  
class GFG 
{
    Node root;
    Max_level max = new Max_level();
  
    // Recursive function to print bottom
    // right view of a binary tree.
    void bottomRightViewUtil(Node node, int level,
                             Max_level max_level)
    {
  
        // Base Case
        if (node == null)
            return;
  
        // Recur for right subtree first
        bottomRightViewUtil(node.right,
                            level, max_level);
  
        // If this is the last Node of its level
        if (max_level.max_level < level) 
        {
            Console.Write(node.data + " ");
            max_level.max_level = level;
        }
  
        // Recur for left subtree with increased level
        bottomRightViewUtil(node.left,
                            level + 1, max_level);
    }
  
    void bottomRightView()
    {
        bottomRightView(root);
    }
  
    // A wrapper over bottomRightViewUtil()
    void bottomRightView(Node node)
    {
  
        bottomRightViewUtil(node, 1, max);
    }
  
    // Driver Code
    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();
    }
}
  
// This code is contributed by Princi Singh

chevron_right


Output:

3 6 4

Time Complexity : O(N), where N is the number of nodes of the binary tree.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.