Tree Top View Problem

How can I approach this problem?
Is it similar like Tree bottom view?

@Kinjal hey, The idea is to do something similar to vertical Order Traversal. 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.In bottom view you update value at particular horizonatl distance but in this you will not update ,the first value will be there for particular horizontal distance.

I have just one test case passed. Can you give a look?

@Kinjal hey your logic is looking fine check the input of tree ,whether it is building using level order or recursion,also please check the output format.

I checked every possible silly mistakes or silly corner cases. So, I dont know what went wrong. Now, i’m counting on you.

@Kinjal ok let me correct your code.

@Kinjal hey I have made changes to yoyr build function and its working fine,please check it:

So it passed all test cases. Can you kindly tell me, what you have done in my code? I know that you made some changes but I need to know the reason, what happened before and what’s happening now!

@Kinjal hey please check the build function ,I have change few conditions you will definitely get it after seeing it,also dry run the build part.

Yes. I have seen the build function as you have mentioned before. So, my doubt is that if I’m updating horizontal distance inside the build function then I think, there is no reason to update same horizontal distance in treeTopView(root) function. Right?

@Kinjal hey ,yes its not necessary that you update horizontal distance in build function,check rest of logic .

But in case, if I’m updating horizontal distance in my build function so there is no reason to update it again so I changed some lines of code in my treeTopView(root) function. But, now output is given wrong, WHY!

@Kinjal hey iyou have to do changes in hd in top view function but you can remove it in build,this is happening because you have taken hd as element of struct,if you take it separately in queue ,then also you have to make changes only in top view not in build function.

No, The reason you’re giving is the exact reason why I’m thinking like this in the first place. So, I believe that when we are updating some nodes information like,

node->data
node->hd
node->left
node->right

it remains saved with the node, I guess. So when I pushed some node inside the queue then actually we pushed all the information about that node as well, right!! I mean, in this case when I push some node inside the queue from the Tree, then I’m also pushed horizontal distance of that node inside the queue from my point of view. So, if node->hd value is saved inside the queue then why should I have to update it again and why not, I just insert into the map in the first place in the treeTopView(root) function. Do you get it, what I’m saying! If I’m wrong, then acknowledge me with your wisdom.

@Kinjal hey both refer to same thing and you are correct ,when you make changes in hd of node then it will reflect in original and if you dont take hd in struct then in queue with each node you have to take their hd and then apply operations as both of them as one elemnt.Hope you get it,try that approach also.

But, in second approach, giving wrong answer.

@Kinjal send me the code,there must be some error.

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.

This is the update, we are talking about. And, in this approach I didn’t get any result.

@Kinjal hey chl to rha hai sahi apne build krte waqt hd update krdia thats also a way ,input sahi dalkr check kro pls.