# Right view of the binary tree in C++

In this tutorial, we will learn about the Right view of the binary tree in C++. We will also see the code implementation to find out the right view of the binary tree.

We will also see the example to understand the concept in a better way.

## Right view of the binary tree

The right view of the binary tree is a set of nodes visible when we see the tree from the right side.

Also learn:

Let’s see the example,

```                         1
/ \
2   3
/  \ /  \
4   5 6   7    right view of tree is 1-3-7.

1
/   \
2      6
\   /   \
3 7     9    right view of tree is 1-6-9-8.
/ \ \
4    5 8```

So, in the above examples, we can see the right view of the binary tree.

We simply use the level order traversal of the binary tree to print the right view.

Let’s see the pseudocode,

1. Take an empty queue–queue<Node*>q.
2. Push the root node into queue.
3. While q is not empty -Pop the node from queue.
-Check the pop node is righmost node or not for this level. if it is rightmost node, then print it. -Push the left child in queue.
-Push the right child in queue.

So, we can see the use of level order traversal in above pseudo code to print the right view.

Let’s see the code for right view of binary tree.

Code implementation in C++ – Right view of binary tree

```#include<iostream>
#include<bits/stdc++.h>
using namespace std;
//structure for tree node
struct Node{
int data;
Node* left;
Node* right;
};
//function to create node
Node* create(int x){
Node* temp=(Node*)malloc(sizeof(Node));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return temp;
}
//function to print the right view
void rightView(Node *root)
{
queue<Node*>q;
q.push(root);
q.push(NULL);
while(!q.empty()){
Node* temp=q.front();
q.pop();
if(temp==NULL){
q.push(NULL);
if(q.front()==NULL)return;
continue;
}
if(q.front()==NULL)cout<<temp->data<<" ";
if(temp->left!=NULL)q.push(temp->left);
if(temp->right!=NULL)q.push(temp->right);
}
}
int main(){
Node* root=create(1);
root->left=create(2);
root->left->right=create(3);
root->left->right->left=create(4);
root->left->right->right=create(5);
root->right=create(6);
root->right->left=create(7);
root->right->left->right=create(8);
root->right->right=create(9);
cout<<"right view of the given binary tree is: ";
rightView(root);
return 0;
}```

Output

`right view of the given binary tree is: 1 6 9 8`

You may also learn,

Boundary traversal of the Binary tree in C++

Find out the Diameter of the Binary Tree in C++