Given an binary tree, print top view of it. Assume, the left and right child of a node makes 45 degree angle with the parent.

For example, Top view of below tree is 2, 1, 3, 6

This problem can be easily solved with the help of Hashing. The idea is to create an empty map where each key represents the relative horizontal distance of the node from the root node and value in the map maintains a pair containing node’s value and its level number. Then we do a pre-order traversal of the tree and if current level of a node is less than maximum level seen so far for the same horizontal distance as current node’s or current horizontal distance is seen for the first time, we update the value and the level for current horizontal distance in the map. For each node, we recurse for its left subtree by decreasing horizontal distance and increasing level by 1 and recurse for right subtree by increasing both level and horizontal distance by 1.

Below figure shows horizontal distance and level of each node in above binary tree. The final values in the map will be

horizontal distance -> (node’s value, node’s level)

-1 -> (2, 2)

0 -> (1, 1)

1 -> (3, 2)

2 -> (6, 3)

**C++ implementation –**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
// Data structure to store a Binary Tree node struct Node { int key; Node *left, *right; }; // Recursive function to do a pre-order traversal of the tree and fill map // Here node has 'dist' horizontal distance from the root of the // tree and level represent level of the node void printTop(Node *root, int dist, int level, auto &map) { // base case: empty tree if (root == nullptr) return; // if current level is less than maximum level seen so far // for the same horizontal distance or horizontal distance // is seen for the first time, update the map if (map.find(dist) == map.end() || level < map[dist].second) { // update value and level for current distance map[dist] = { root->key, level }; } // recurse for left subtree by decreasing horizontal distance and // increasing level by 1 printTop(root->left, dist - 1, level + 1, map); // recurse for right subtree by increasing both level and // horizontal distance by 1 printTop(root->right, dist + 1, level + 1, map); } // Function to print the top view of given binary tree void printTop(Node *root) { // create an empty map where // key -> relative horizontal distance of the node from root node and // value -> pair containing node's value and its level map<int, pair<int, int>> map; // do pre-order traversal of the tree and fill the map printTop(root, 0, 0, map); // traverse the map and print top view for (auto it: map) cout << it.second.first << " "; } |

The time complexity of above solution is O(nlog(n)) and auxiliary space used by the program is O(n).

**Exercise:** Modify the solution to print top view of binary tree

**Thanks for reading.**

Please use ideone or C++ Shell or any other online compiler link to post code in comments.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply

A O(n) solution. The idea is same but the implementation is different.

http://cpp.sh/9mtp

Hey, the example output looks incorrect.

Thanks a lot for bringing this issue to our notice. We have updated the example output. Happy coding 🙂