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. Expected time complexity is O(n)
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(n) where n is the number of nodes in the given binary tree. The assumption here is that add() and contains() methods of HashSet work in O(1) time.
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print nodes in the Top View of Binary Tree | Set 3
- Print nodes in top view of Binary Tree | Set 2
- Sum of nodes in top view of binary tree
- 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 Right View of a Binary Tree
- Print Left View of a Binary Tree
- Print Bottom-Right View of a Binary Tree
- Iterative Method To Print Left View of a Binary Tree
- Print all even nodes of Binary Search Tree
- Print the nodes of Binary Tree having a grandchild
- Print all internal nodes of a Binary tree
- Print Levels of all nodes in a Binary Tree
- Print the nodes having exactly one child in a Binary tree
- Print all full nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree | Set 2
- Print all nodes between two given levels in Binary Tree
- Print all nodes in a binary tree having K leaves