Right view of a binary tree in O(1) extra space

Hi all please someone help me in this.
How to find the right view of a binary tree in O(1) extra space.

Traverse the tree to its right side only -

void traverse(struct node *ptr){
if(ptr==NULL){
return;}
cout<data;
traverse(ptr->rchild);

Your solution is not correct. Consider the given below example your code will print only 1 but output should be 1 and 2.
1
/
2

but we need to print only the right half, and 2 is on left side (isn’t is so), therefore output should be 1 only

We are asked to print right view not right half. Suppose assume you are standing to the right of a given tree you need to print all those nodes which are visible. Please refer gfg for further clarification and also there are few similar problems like top view , bottom view and left view in leetcode once try to solve them.

1 Like

oh ! sorry, I got your point. I will try them, thanks