Top view of the binary tree

i was trying this question all testcases are not passing
question–>
https://practice.geeksforgeeks.org/problems/top-view-of-binary-tree/1#
code–>

hello @sheikhhaji18

image

ur code will fail for tree like this becuase the node in right bottom will get visited first then the the node on the top of it. which will produce wrong result.

to remove this->
store level of node as well and insert/update map
only when key is not present in map or when the level of current node is lesser than level of node present in map

https://ide.codingblocks.com/s/376389 thank @aman212yadav
passed all test case
similarly for bottom view
we have insert values for keys(horizontal distance) which are not present in the map
and when level of the current is greater than that of present in the hashmap
the hash map is defined as
key–>horizontal distance
value–>root->data, level

yeah similar logic will be applied there as well

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.