Any common means/methodolgy to understand "how to find all views of binary tree " in PYTHON ?
- Left
- Right
- Bottom
- Top
Any common means/methodolgy to understand "how to find all views of binary tree " in PYTHON ?
LEFT:
do a level order traversal on the tree and print the leftmost node at every level. Below is the implementation of above approach:
RIGHT:
we will do a level order traversal on the tree and print the rightmost node at every level.
Below is the implementation of above approach:
BOTTOM:
Create a map like, map where key is the horizontal distance and value is a pair(a, b) where a is the value of the node and b is the height of the node. Perform a pre-order traversal of the tree. If the current node at a horizontal distance of h is the first we’ve seen, insert it in the map. Otherwise, compare the node with the existing one in map and if the height of the new node is greater, update in the Map.can see this:
TOP:
Like vertical Order Traversal, we need to put nodes of same horizontal distance together. We do a level order traversal so that the topmost node at a horizontal node is visited before any other node of same horizontal distance below it. Hashing is used to check if a node at given horizontal distance is seen or not.