Right view of Binary Tree using Queue
Last Updated :
19 Jul, 2022
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.
Examples:
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.
We have already discussed recursive solution for right view. In this post, level order traversal based solution is discussed.
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:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
Node* newNode( int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void printRightView(Node* root)
{
if (!root)
return ;
queue<Node*> q;
q.push(root);
while (!q.empty())
{
int n = q.size();
for ( int i = 1; i <= n; i++)
{
Node* temp = q.front();
q.pop();
if (i == n)
cout<<temp->data<< " " ;
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
}
}
}
int main()
{
Node* root = newNode(10);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(7);
root->left->right = newNode(8);
root->right->right = newNode(15);
root->right->left = newNode(12);
root->right->right->left = newNode(14);
printRightView(root);
}
|
Java
import java.util.*;
public class PrintRightView
{
private static class Node
{
int data;
Node left, right;
public Node( int data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
private static void printRightView(Node root)
{
if (root == null )
return ;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty())
{
int n = queue.size();
for ( int i = 1 ; i <= n; i++) {
Node temp = queue.poll();
if (i == n)
System.out.print(temp.data + " " );
if (temp.left != null )
queue.add(temp.left);
if (temp.right != null )
queue.add(temp.right);
}
}
}
public static void main(String[] args)
{
Node root = new Node( 10 );
root.left = new Node( 2 );
root.right = new Node( 3 );
root.left.left = new Node( 7 );
root.left.right = new Node( 8 );
root.right.right = new Node( 15 );
root.right.left = new Node( 12 );
root.right.right.left = new Node( 14 );
printRightView(root);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
self .hd = 0
def printRightView(root):
if ( not root):
return
q = []
q.append(root)
while ( len (q)):
n = len (q)
for i in range ( 1 , n + 1 ):
temp = q[ 0 ]
q.pop( 0 )
if (i = = n) :
print (temp.data, end = " " )
if (temp.left ! = None ) :
q.append(temp.left)
if (temp.right ! = None ) :
q.append(temp.right)
if __name__ = = '__main__' :
root = newNode( 10 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 7 )
root.left.right = newNode( 8 )
root.right.right = newNode( 15 )
root.right.left = newNode( 12 )
root.right.right.left = newNode( 14 )
printRightView(root)
|
C#
using System;
using System.Collections.Generic;
public class PrintRightView
{
private class Node
{
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
private static void printRightView(Node root)
{
if (root == null )
return ;
Queue<Node> queue = new Queue<Node>();
queue.Enqueue(root);
while (queue.Count != 0)
{
int n = queue.Count;
for ( int i = 1; i <= n; i++)
{
Node temp = queue.Dequeue();
if (i == n)
Console.Write(temp.data + " " );
if (temp.left != null )
queue.Enqueue(temp.left);
if (temp.right != null )
queue.Enqueue(temp.right);
}
}
}
public static void Main(String[] args)
{
Node root = new Node(10);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(7);
root.left.right = new Node(8);
root.right.right = new Node(15);
root.right.left = new Node(12);
root.right.right.left = new Node(14);
printRightView(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
function printRightView(root)
{
if (root == null )
return ;
var queue = [];
queue.push(root);
while (queue.length != 0)
{
var n = queue.length;
for ( var i = 1; i <= n; i++)
{
var temp = queue.shift();
if (i == n)
document.write(temp.data + " " );
if (temp.left != null )
queue.push(temp.left);
if (temp.right != null )
queue.push(temp.right);
}
}
}
var root = new Node(10);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(7);
root.left.right = new Node(8);
root.right.right = new Node(15);
root.right.left = new Node(12);
root.right.right.left = new Node(14);
printRightView(root);
</script>
|
Time Complexity: O( n ), where n is the number of nodes in the binary tree.
Auxiliary Space: O( n ) for Queue Data Structure
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...