Why this code isn’t even printing inorder and postorder. Then, I guess it will work fine in printing bottom view
Tree bottom view
@mohitsethia
your bottomview() function is correct, it is giving correct output but your buildtree() function is wrong. You are inserting all nodes at the left side of your tree. Dry run your buildtree() for sample testcase.
Oh I got it, but then how can we take input without using queue, is there any way?
In the given input format, the left child of ith index is at (2i+1) and right child is at(2i+2)th index. If ar[i] is -1 that is NULL, it don’t have child nodes.
I got it, but in a confusion how to implement it, can you tell me how to do it more briefly.
And I implemented using queue and one test case failed.
wrong answer in the test case 20 8 22 7 3 4 25 -1 -1 10 14 -1 -1 -1 -1 -1 -1 -1 -1
But I am printing the top element of each vertical line which is the bottom element, still got wrong answer
@mohitsethia
your tree construction is correct now, also the way you are storing nodes in map and printing last element is also correct. But instead to using dfs use bfs.
How to do that?
Please answer in a more elaborated way.
@mohitsethia
reate queue of pair<node*,int> , first element of pair denotes node and second element denotes it’s distance from root node. Initially insert (root_node,0) to your queue and run loop till queue is not empty. Inside this loop, first take front element of queue and pop it. Check for left child if exist insert it into queue with distance = d-1 and also insert it in your map. Similarly check for right child, if exists insert into queue with distance d+1 and also insert in you map.