Vertical tree problem

i am not able to code for this problem. can you tell me how to do it.

https://hack.codingblocks.com/contests/c/917/961 - this is the url of the question.

https://ide.codingblocks.com/s/107889 - and this is the question in case the question link is not opening.

We can do preorder traversal of the given Binary Tree. While traversing the tree, we can recursively calculate HDs. We initially pass the horizontal distance as 0 for root. For left subtree, we pass the Horizontal Distance as Horizontal distance of root minus 1. For right subtree, we pass the Horizontal Distance as Horizontal Distance of root plus 1. For every HD value, we maintain a list of nodes in a hash map. Whenever we see a node in traversal, we go to the hash map entry and add the node to the hash map using HD as a key in map.