Please share how the tree bottom is being printed
Tree bottom view
Hi @Faizan-Ali-1395131367301898
The following are steps to print Bottom View of Binary Tree.
- We put tree nodes in a queue for the level order traversal.
- Start with the horizontal distance(hd) 0 of the root node, keep on adding left child to queue along with the horizontal distance as hd-1 and right child as hd+1.
- Also, use a TreeMap which stores key value pair sorted on key.
- Every time, we encounter a new horizontal distance or an existing horizontal distance put the node data for the horizontal distance as key. For the first time it will add to the map, next time it will replace the value. This will make sure that the bottom most element for that horizontal distance is present in the map and if you see the tree from beneath that you will see that element.