In this post, we will discuss a method to print the Right view of a binary tree. This is one the most common easy level interview questions. Also, you might get to see interview questions which can be solved with slight variations to this code. The approach we take here is almost very similar to printing the Left view of the Binary Tree
The right view contains all nodes that are last nodes in their levels. Right View of a tree is defined as the nodes that would be visible to us from the right side. To do this we have to simply print the first node encountered while traversing a level from the right. If you have seen my post for the left view of the binary tree then the only change that we make here is to traverse the right child of the current node first and then traverse the left node. Have a look at the below image to get some idea.
There are 2 solutions to this problem, first is to do a recursive preorder traversal in which we maintain max level we have traversed and then print the node if the max level is less than the max_level variable and update the max_level variable, another simple solution is to do level order traversal and print the first node in every level but instead of doing the standard preorder traversal we just visit the Right child first and then the left child.
Method 1: Using Recursive Preorder traversal
Most of the problems related to trees can be solved with little modifications to the basic preorder traversal code. For this method, we do the following steps.
Method 2: Using BFS traversal (using queue)
This is an iterative approach in which we use a queue to store the nodes in the next level and then print the leftmost node in each level.
Left View of a Binary Tree
The right view contains all nodes that are last nodes in their levels. Right View of a tree is defined as the nodes that would be visible to us from the right side. To do this we have to simply print the first node encountered while traversing a level from the right. If you have seen my post for the left view of the binary tree then the only change that we make here is to traverse the right child of the current node first and then traverse the left node. Have a look at the below image to get some idea.
Right View of a Binary Tree |
There are 2 solutions to this problem, first is to do a recursive preorder traversal in which we maintain max level we have traversed and then print the node if the max level is less than the max_level variable and update the max_level variable, another simple solution is to do level order traversal and print the first node in every level but instead of doing the standard preorder traversal we just visit the Right child first and then the left child.
Method 1: Using Recursive Preorder traversal
Most of the problems related to trees can be solved with little modifications to the basic preorder traversal code. For this method, we do the following steps.
- We create a variable max_level that denotes the max level traversed and we create a variable level that denotes the current level being traversed.
- We check if the level of the current node is greater than the max_level. if yes then we print the current node and update the max_level else we move ahead.
- In each recursive call, we increase the current level by one as we travel down the binary tree.
- Remember to traverse the right child first and then the left child.
Let's have a look at the code
C++ Program
Sample input and output to check the program
Method 2: Using BFS traversal (using queue)
This is an iterative approach in which we use a queue to store the nodes in the next level and then print the leftmost node in each level.
- Create an empty queue and put the head node into it.
- Create a max_level variable to denote the current level being traversed, and a variable current_node to denote the current node being processed.
- If the level of the current_node is greater than the max_level then do the following steps:
- print the value of the current_node.
- set max_level to the level of the current node.
- Enqueue the right child of the current node and then the left child of the current_node in the queue.
- Do the steps 2 to 4 till queue is empty.
Let's have a look at the code
You might also be interested in C++ Program
Sample input and output to check the program
Left View of a Binary Tree
Comments
Post a Comment