Question link: https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/practice-problems/algorithm/gandhi-tree-march/description/
i am having difficulty in writing the code..i am thinking about implementing it with stack.
Asked by: Samrat_De on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.
I guess you have found out how the tree is built...
Now you just have to traverse the tree....By maintaining a column counter ...
and maintain a map of type <int,vector<char>> globally....
Now before Moving to the left subtree decrease the counter by 1 and after the left subbranch is completed and you backtrack to the same function increase the counter again(as you reach the same node).... same goes for the right subtree(increase it first and then decrease it when you backtrack the recursion)...
And as you visit the nodes ...add the nodes to the map to the key equal to the value of counter...
Hope this helps...