Tree_bottom_view_problem


I am still not able to understand how the output is coming out to be 4 2 6 3. Because in the level order print it is clear that 5 can also be in the output. So I think it should be 4 2 5 6 3.

Hello,
To understand this output you have to imagine the physical structure of the tree.

For the given input set, the tree formed would look like following diagram:

________1
____2_______3
__4_____5
________6

What is happening here is 1,5 and 6 are co-linear i.e. they all lie on the same line.
As 6 comes after 5, thus 6 overlaps the view of 5 from bottom.

Hence, output is 4 2 6 3.

Hope, this would help.

But 4,5,6 are in the same level and you have shown that 6 is in the next level

Yes,
4, 5,6 are on the same level. But it is longitudinal/horizontal view.
Now, try to see this tree vertically.

What is happening is, 5 and 6 are sharing the same coordinates or in simple words, they are at same position, diagrammatically. Thus, when 6 comes, it takes the same position where 5 is lying.
Hence, 5 gets overlapped by 6.