Tree bottom view

I am getting the wrong answer in one test case. My code is https://ide.codingblocks.com/s/219971

you should update your map traversing the tree level wise NOT preorder traversal.
think about the case when left child of root has a large right subtree than right node of root.
eg. 1 2 3 -1 4 -1 5 -1 -1 -1 -1
in this case the answer should be 2 4 5 but your code will produce 2 4 3 which is wrong. try to draw this tree and realize yourself.
solutions: do level order traversal instead of preorder.

thanks

1 Like