5.Tree : Top View

Top view means when you look the tree from the top the nodes, what you will see will be called the top view of the tree.

       1
    /     \
   2       3
  /  \    / \
 4    5  6   7
Top view of the above binary tree is
4 2 1 3 7




        1
      /   \
    2       3
      \   
        4  
          \
            5
             \
               6
Top view of the above binary tree is
2 1 3 6

Solution:

This solution is based on level order traversal and vertical order traversal using maps.

Time Complexity:- O(n log(n) )

Last updated

Was this helpful?