Skip to main content

Posts

Showing posts from July, 2018

Bottom View of a Binary Tree

In this post, we will discuss a method to print the Bottom 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. We will use a method that is very similar to printing Top View of a Binary Tree as discussed in our last post.

Bottom View of a tree is defined as the nodes that would be visible to us if we viewed the tree from the bottom. To do this we have to maintain a horizontal distance of all the nodes from the root node and print the last node for each horizontal distance. This problem can be solved easily with simple traversal and use of data structures. Have a look at the below image to get some idea.



In the above image, the horizontal distance of the root is set to 0 and for all the node to the right we increase the horizontal distance by 1 and for all the node to the right, we decrease the horizontal distance by 1. Thus, the problem reduces to printi…

Right view of a Binary Tree

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 n…

Left view of a Binary Tree

In this post, we will discuss a method to print the left 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.
First, let's see what do we mean by the left view of a binary tree.The left view contains all nodes that are first nodes in their levels. Left View of a tree is defined as the nodes that would be visible to us from the left side. To do this we have to simply print the first node encountered while traversing a level from the left. 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.

Method 1: Using Recursive Preor…