We break the problem in 3 parts:
- Print the left boundary in top-down manner.
- Print all leaf nodes from left to right, which can again be sub-divided into two sub-parts:
Print all leaf nodes of left sub-tree from left to right.
Print all leaf nodes of right sub-tree from left to right.
- Print the right boundary in bottom-up manner.
The left most node is also the leaf node of the tree.
Input :
1
/ \
2 3
/ \ \
4 5 6
Output : 1 2 4
Input :
1
/ \
2 3
\
4
\
5
\
6
Output :1 2 4 5 6
Right view of following tree is 1 3 7 8
1
/ \
2 3
/ \ / \
4 5 6 7
\
8