Tree top view BInary Tree

Does this question also require the knowledge of maps??

using map this question become easy
but you can also do this without map

Approach

  1. This approach is based on the level order traversal. We’ll keep record of current max so far left, right horizontal distances from the root.
  2. And if we found less distance (or greater in magnitude) then max left so far distance then update it and also push data on this node to a stack (stack is used because in level order traversal the left nodes will appear in reverse order), or if we found greater distance then max right so far distance then update it and also push data on this node to a vector.
  3. In this approach, no map is used.

Reference Code

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.