Top view of a binary tree (Algorithm)

preview_player
Показать описание
Print the top view of a Binary Tree. We use level order traversal and vertical order traversal for this.

Рекомендации по теме
Комментарии
Автор

If you are confused why level order traversal is required then please read this:
Because when you are traversing for the vertical order traversal, let say you move left of the root first then it will scan all the left nodes of the root before moving on to the root's right node. In these scans if there is a node which has Hd > 0 then it will first add this node to the map's key -> linked list. So the first node of the linked list is not guaranteed to be the top viewed element. In the video example, there is no such element but you can consider a new node added to the right of e, let say x for eg. Hd of x will be 1 and it will be in the linked list before you will scan c. But you want to print c as a top view and not x. That's why level order traversal is also necessary in this case.
I hope this is clear.

karansvnit
Автор

very well done, excellent video. subscribed!

I am personally very picky with youtube tutorials and would almost always rather struggle my way through a problem, rather than watch a 2 min of a low quality tutorial. Your tutorials however are very good. Great sound, great quality and great teaching, very well spoken.

Thank you for this, great job! and subscribed. you deserve it! (:

ianchui
Автор

You are true Vivekanand of 21st Century
Thanks for excellent video

pratyushharsh
Автор

At 6:17 why did you ask to check which come first in the level order traversal. is not it a extra step? its already at the top?

Bansalvks
Автор

Excellent explanation, it helped me to understand in a simple way.

oJoao
Автор

Both the Bottom View and Top View of Binary Tree are nicely explained.
Please come up with more data structure and algorithms problems.

rohanrampuria
Автор

Sir, Please post code link also in description. It will help us understanding better.

pradeepyadav
Автор

Finally got a beautiful explaination for this problem was unable to crack the question statement!

ditu-kunalsarda
Автор

I hope you start posting again. You're a great teacher!! Promote your classes and gain more subscribers. You deserve a million subscriber!

axbax
Автор

If we do inorder traversal and at the same time, put the root->data in TreeMap then at the end will have to print every first element of the TreeMap for each hd. As TreeMap maintains increasing order of value we can simply traverse the treeMap at the end.

nishailame
Автор

appreciating your work. Thank you so much for the video.

sureshbobby
Автор

you explained very clearly, thank you.

tugce
Автор

@Author: Please include a working code github URL in the description. Helps to verify our algorithm.

prateeksharma
Автор

In each level minimum and maximum horizontal distance, will be the topview.

surajpant
Автор

wuah its great ! simple and to the point
well there is one request please discuss the time complexity and space complexity also in end ☺

payalsagar
Автор

great explanation...now I will always remember the solution..Thanks A lot !!
:)

funnyandamazingvideos
Автор

Thank you so much sir🙏... Well explained

successally
Автор

Can you explain why do we need to store level order traversal of the tree? Why can't we store the values in a map where the key is hd and value is a linked list of nodes. Later we just print the first element for every key.

nayankurude
Автор

Solution is simple. What will be algorithmic complexities for this method? I think Stack and Queue combination will give better performance in terms of time and space complexity.

KNT_
Автор

Hi Vivekanand, the videos are great. Can you please add the link to the code related to the programs, it would be helpful. Thank you

pramodsripada