Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can be printed in any order.
A node x is there in output if x is the topmost node at its horizontal distance. Horizontal distance of left child of a node x is equal to horizontal distance of x minus 1, and that of right child is horizontal distance of x plus 1.
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
The idea is to do something similar to vertical Order Traversal. Like vertical Order Traversal, we need to put nodes of same horizontal distance together. We do a level order traversal so that the topmost node at a horizontal node is visited before any other node of same horizontal distance below it. Hashing is used to check if a node at given horizontal distance is seen or not.
Following are nodes in top view of Binary Tree 2136
This approach does not require a queue. Here we use the two variables, one for vertical distance of current node from the root and another for the depth of the current node from the root. We use the vertical distance for indexing. If one node with the same vertical distance comes again, we check if depth of new node is lower or higher with respect to the current node with same vertical distance in the map. If depth of new node is lower, then we replace it.
Following are nodes in top view of Binary Tree 2 1 3 6
This approach is contributed by Akash Debnath
Time Complexity of the above implementation is O(nlogn) where n is the number of nodes in the given binary tree with each insertion operation in Map requiring O(log2n) complexity.
This article is contributed by Rohan. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print nodes in top view of Binary Tree | Set 2
- Print nodes in the Top View of Binary Tree | Set 3
- Sum of nodes in top view of binary tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Sum of nodes in the right view of the given binary tree
- Sum of nodes in the left view of the given binary tree
- Sum of nodes in bottom view of Binary Tree
- Print Left View of a Binary Tree
- Print Right View of a Binary Tree
- Iterative Method To Print Left View of a Binary Tree
- Print Bottom-Right View of a Binary Tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Bottom View of a Binary Tree
- Right view of Binary Tree using Queue
- Bottom View of a Binary Tree using Recursion
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Print nodes between two given level numbers of a binary tree
- Print leftmost and rightmost nodes of a Binary Tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Print all nodes in a binary tree having K leaves