Problem Statement:
Given a binary tree, print the left and right view of the tree.
For example:
If you have a tree as below:
The left view will be a, b, d, h and right view will be a, c, g, j. As highlighted in image below.
So how do we solve this problem?
We can easily solve it by 2 steps
1. Get the level order traversal
2. The starting node for all the level order traversal will be the left view
3. The ending node at each level will be the right view.
Step 1: The level order traversal for the above binary tree will be:
a,
b, c
d, e, f, g
h, i, j
For left view get the first node at each level:
a, b, d, h
For right view get the last node at each level
a, c, g, j
Hence the solution.