Print The Top View of a Binary Tree

Objective: Given a binary tree, print it in the Top View of it.

What is Top View: Top view means when you look at the tree from the top the nodes you will see will be called the top view of the tree. See the example below.

Print The Top View of a Binary Tree.
Print The Top View of a Binary Tree.

as you can see in the example above,8, 4, 2, 1, 3, 7 is the Top view of the given binary tree.

Approach:

This approach is quite similar to the Print the Binary Tree in Vertical Order Path. Just modified the code so that it will print only the first element it will encounter in the vertical order.

Read more