Тёмный

Binary Tree Level Order Traversal - Drawing The Parallel Between Trees & Graphs 

Back To Back SWE
Подписаться 242 тыс.
Просмотров 49 тыс.
50% 1

Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe....
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: You are given the root node of a binary tree. Return a list consisting of each level of the tree (we will have a list of lists, a list for each level's items), where each level is traversed from left to right. A level order traversal.
What Do We Know?
We know our preorder, inorder, and postorder traversals...but this is a little different.
We want to go level by level here...what does this remind us of?
Every acyclic connected graph is a tree, and all trees are acyclic connected graphs.
This unlocks our ability to search a tree list we search a graph, using Breadth First Search and Depth First Search.
DFS will go deep, and BFS will go out level by level. We want BFS since it is a more natural approach to something like this.
We realize that this is just the breadth-first search of a tree structure.
We shift our knowledge from graph search to this problem.
Complexities
n is the total amount of nodes in the binary tree
Time: O( n )
Space:
With output: O( n ) because we will store n nodes in the list of levels structure.
Without output: O( n ) the queue at maximum at any point in time will hold some fractional component of the total nodes in the tree.
The fractional constant disappears and the asymptotic behavior is linear.
We can't bound the max space in the queue to the widest level because when we process the widest level...if there is a level below it we will have all the nodes from the widest level PLUS what we are adding into the queue as we process each node in the level (we remove 1 node and can add 2 children which will push us past the size of the widest level initially).
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: / @hackerrankofficial
Tuschar Roy: / tusharroy2525
GeeksForGeeks: / @geeksforgeeksvideos
Jarvis Johnson: / vsympathyv
Success In Tech: / @successintech

Опубликовано:

 

4 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 133   
@BackToBackSWE
@BackToBackSWE 5 лет назад
Table of Contents: A Quick Message 0:00 - 1:01 The Problem Introduction 1:01 - 1:41 Starting With What We Know? 1:41 - 2:34 How Do We Transition To Level By Level? 2:34 - 2:41 Connecting Trees & Graphs (no pun intended) 2:41 - 3:04 Do You See A Graph Or A Tree? 3:04 - 3:53 Notice That This Is The Same Tree 3:53 - 5:07 Back To The Original Tree 5:07 - 5:50 We Realize Breadth First Search Is Most Natural 5:50 - 6:25 What Do We Use For Breadth First Search 6:25 - 7:22 Walking Through The Breadth First Search 7:22 - 11:22 The Breadth First Search Is Finished 11:22 - 12:03 Wrap Up 12:03 - 12:36 I Forgot Complexities -> Let n be the # of nodes in the binary tree. Time complexity is O(n) since we process n nodes and perform O(1) work per node processed. Space complexity is O(n) if we put all nodes in an array of arrays (each level gets its own array). Space is also O(n) if the output is not included since the upper bound on the number of items in the queue at any one moment in time will be a fractional component of n. The fractional constant disappears and yields O(n) space. Space scales in a linear fashion with the input size. The code for this problem is in the description. Fully commented for teaching purposes.
@Azx499
@Azx499 4 года назад
I'm not finding the code in the description
@BackToBackSWE
@BackToBackSWE 4 года назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-lw3Or6eqIpI.html
@Azx499
@Azx499 4 года назад
Back To Back SWE lol
@jenny3416
@jenny3416 4 года назад
@@Azx499 Same
@danielebanana
@danielebanana 2 года назад
Where is the code? I can't find in the description
@vasachisenjubean5944
@vasachisenjubean5944 4 года назад
you are among the few people who keep DSA interesting
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks haha
@matthewmolinar
@matthewmolinar 2 года назад
fax
@prithazz
@prithazz 4 года назад
I feel like you are my BFF during interview prep. Otherwise, I would be crying by myself
@BackToBackSWE
@BackToBackSWE 4 года назад
sure if u want
@IChowdhury01
@IChowdhury01 3 года назад
I'm crying even with these videos. Can't do any tree/graph LC mediums.
@siddhantpathak3811
@siddhantpathak3811 3 года назад
People get friendzoned even when not asked lol
@nedwilliams8806
@nedwilliams8806 3 года назад
For me, it clicks. Recursion, BFS, Binary Structures. They're all just clicking thanks to you. Your style of working it out step by step is exactly what I do at home, I have a couple of white boards as well. But your way of looking at these problems has really solidified my understanding. Hats off, sir.
@yicai7
@yicai7 4 года назад
The GREATEST explanation about binary tree level order
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@svdfxd
@svdfxd 5 лет назад
As usual, great video. Like the way you explain things by a little revision of topics already learnt e.g. queues used for BFS, stacks used for DFS etc. Keep making these awesome videos to cover all the interview related topics/ questions.
@BackToBackSWE
@BackToBackSWE 5 лет назад
Workin on it. I wish this could be my full-time job for a little. Too much work ahead.
@brandonelzy4196
@brandonelzy4196 4 года назад
Really amazing explanation. Your tutorials are invaluable. I really appreciate this. Thank you.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@jingjing6813
@jingjing6813 5 лет назад
Thank you so much for this content. I like the way that you explain logic clearly. I appreciate your time to do this. Thanks Ben.
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure, we have a long way to go.
@ObsessedAwe
@ObsessedAwe 2 года назад
May God bless this man beyond his wildest dreams.
@tannerbarcelos6880
@tannerbarcelos6880 4 года назад
I have a interview at SAP next Wednesday. It’s for their new grad talent program. I applied and did an assessment for frontend, and got through that and the first interview. However now it’s technical. I’m not really sure if it’s gonna be related to frontend and such or just basic SWE interview stuff. That said, for a new grad type of role, typically what should someone like me, interviewing for such a role AND never interviewing before, be focusing on? Thank you so much brother. You explained this algorithm amazingly. I see tons of problems requiring BFS / Level Order stuff
@BackToBackSWE
@BackToBackSWE 4 года назад
I don't think I'm well positioned to answer this, you should as others who interviewed (if you can get a hold of them)
@matthall8555
@matthall8555 5 лет назад
Well done good sir!!! You drastically reduce the amount of time needed to study these problems!!!
@BackToBackSWE
@BackToBackSWE 5 лет назад
haha nice
@lsamparkl
@lsamparkl 4 года назад
this is so insightful other videos are just plainly teaching how the algorithm works. I also like how you derived at that solution. Ideation is such an important skill to improve upon. I feel like no one does that when explaining the solution.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@jpphoopha
@jpphoopha 3 года назад
This man is an outstanding teacher.
@amilaiddamalgoda9916
@amilaiddamalgoda9916 4 года назад
One of the best explanations by far! keep it up 👍
@BackToBackSWE
@BackToBackSWE 4 года назад
ye
@rakeshreddyabbireddy8876
@rakeshreddyabbireddy8876 4 года назад
Your teaching is like, hey I don't teach coding but force your brain to code on your own after this lesson. Really you are brain friendly. I think in future you are going to make many new concepts on this channel
@BackToBackSWE
@BackToBackSWE 4 года назад
lol, I am brain friendly lmao great
@nurlanikhsanov6531
@nurlanikhsanov6531 Год назад
Man, you are an amazing teacher! Please, keep going!
@OllytheOzzy
@OllytheOzzy 4 года назад
Dude you make it all so so so clear and easy to understand. Thankyou so much
@BackToBackSWE
@BackToBackSWE 4 года назад
ye
@KPsparks
@KPsparks 4 года назад
You do sucha great job explaining concepts!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@arinmis
@arinmis 3 года назад
Just awesome. You tell those things too clear and it makes this perfect.
@mehkiheath9551
@mehkiheath9551 4 года назад
Such a great teacher!
@BackToBackSWE
@BackToBackSWE 4 года назад
thank you!
@prashant211087
@prashant211087 4 года назад
First video with 0 disklies. Great
@BackToBackSWE
@BackToBackSWE 4 года назад
ye
@bhavishahadiyal7836
@bhavishahadiyal7836 3 года назад
Brooo you are the best I'll definitely help this channel after cracking my interview
@yuselina8769
@yuselina8769 3 года назад
Thank you for all your videos!!
@avery2260
@avery2260 2 года назад
Your videos are amazing and so helpful! Please keep them coming!
@jerrick.warren
@jerrick.warren 5 лет назад
My dude.. that was an amazing video. I appreciate you. Thanks.
@BackToBackSWE
@BackToBackSWE 5 лет назад
ye
@abdoulbarry8111
@abdoulbarry8111 3 года назад
Dude should be paid $millions to teach Data Structures!! idk which college gonna discover this man first omg i Wish you were my professor haha
@MohitSinha4
@MohitSinha4 4 года назад
Thats an amazing video, it really helped me to understand BFS.
@BackToBackSWE
@BackToBackSWE 4 года назад
excellent
@NadyaPena-01
@NadyaPena-01 4 года назад
Thank you so much for this explanation.
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@ethanz4928
@ethanz4928 3 года назад
not seen the code in the description.
@milanpatel6240
@milanpatel6240 3 года назад
You are simply awesome. You made it so simple
@MiguelMartinez-xx2zy
@MiguelMartinez-xx2zy 5 лет назад
You have a noble mission sir! thank you for the great video.
@BackToBackSWE
@BackToBackSWE 5 лет назад
ye
@tajrinkashem1416
@tajrinkashem1416 3 года назад
i just wanna hug you .you are the best man
@danielzheng7673
@danielzheng7673 3 года назад
Very good video and easy understanding explanation. Thumb up!
@carloslazarin
@carloslazarin 2 года назад
Thank you!! great explanation!
@sakshamsrivastava6280
@sakshamsrivastava6280 3 года назад
Thank you for this, helped me visualize it
@Shino_Aizawa
@Shino_Aizawa Год назад
thank you for a great explanetion
@chbh3
@chbh3 4 года назад
lol, thumbnail photo ! this was helpful, ty
@BackToBackSWE
@BackToBackSWE 4 года назад
my b - older me was whack
@curiositykeeda8392
@curiositykeeda8392 5 лет назад
hey man! Firstly I would like to say that, I am a great admirer of ur work ...u r really doing something which matters a lot. I would like to know that if there is any Email id where I can drop u the mails, actually my placements r coming ...it will be starting from July ...so I am kinda a nervous , I am watching ur videos daily and i am learning alot...i will really appreciate if u can guide me what to do.. and lastly... Is this the pseudo code for the level order traversal is correct?? while(queue is NotNULL) { p =sizeof(queue); while(p!=0) { print(front) enqueue(front->left) enqueue(front->right) dequeue(front) p-- } }
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks and I'm not sure there is much I can do to help with your placements. And there is code in the description
@kyokokirigiri166
@kyokokirigiri166 2 года назад
This man is amazing
@anshikagarg7383
@anshikagarg7383 4 года назад
You help alot Sir,THANKYOU SOO MUCH FOR THE VIDEOS.
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@sanketatmaram
@sanketatmaram 2 года назад
Great explanation
@ryananstotz8315
@ryananstotz8315 3 года назад
It seems like you removed the link to the code in the description?
@sofiullahiqbalkiron6818
@sofiullahiqbalkiron6818 3 года назад
A Great Teacher. Thanks a lot.
@deroytanaka
@deroytanaka 5 лет назад
Hey first time viewer, so far I'm liking the content do you have a playlist that has the videos in like sequential order. like the concepts will build off the previous video. thanks
@BackToBackSWE
@BackToBackSWE 5 лет назад
Hey. I wish I did. I mean, I just do whatever questions I find immediately interesting or pull from a question list I have. I have channel playlists but I ran out of those, I can't make anymore.
@deroytanaka
@deroytanaka 5 лет назад
Back To Back SWE no problem 🤙🏾 I'll just watch all the concepts idk as well
@jkim5118
@jkim5118 5 лет назад
These videos are awesome. It seems like knowing the patterns and having the toolsets to solve any kind of problem is important (as you always mention). When was the moment that you had the feeling that you had all these toolsets to apply to any kind of problem?
@BackToBackSWE
@BackToBackSWE 5 лет назад
I still don't think I have "all [these] toolsets" but I guess it is when you immediately know what type of problem a problem is. Noticing dynamic programming immediately. Noticing a BFS quickly. Knowing if the solution can be done with 2 pointers in O(n) time. I'm really not THAT good...I'm ok...but yeah.
@jkim5118
@jkim5118 5 лет назад
@@BackToBackSWE Nice. I am still struggling with many questions, but at least I know how to approach it. What is the next step after "Knowing which toolset to use"?
@BackToBackSWE
@BackToBackSWE 5 лет назад
@@jkim5118 Just having done a lot of questions so that approaches come to mind. It is really all about experience. Intuitions can only go so far.
@priyatanwar8905
@priyatanwar8905 4 года назад
@Back To Back SWE - Awesome video !! I have one question, you mentioned in the description that the space complexity without output is also o(n), The fractional constant disappears and the asymptotic behavior is linear. We can't bound the max space in the queue to the widest level because when we process the widest level...if there is a level below it we will have all the nodes from the widest level PLUS what we are adding into the queue as we process each node in the level (we remove 1 node and can add 2 children which will push us past the size of the widest level initially). My question is, will it always be slightly less than O(n) as never we will have all nodes stored in level and in queue? Could you please elaborate further
@BackToBackSWE
@BackToBackSWE 4 года назад
I want to read and reply but speed replying im sorry I love you
@priyatanwar8905
@priyatanwar8905 4 года назад
@@BackToBackSWE haha well I luv u 2 and waiting for ur reply :D
@fredwooten14
@fredwooten14 5 лет назад
Great work!
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@AhmedAli-jx9ie
@AhmedAli-jx9ie 3 года назад
there is no code in the description
@GoatTwinkie
@GoatTwinkie 3 года назад
holy shit this makes so much sense
@yathanshtewatia2104
@yathanshtewatia2104 3 года назад
I did this exact same thing when I wanted to print out a visual representation of a BST. If i would have watched this video it would not have taken as long as it did figuring this out myself
@harrietread1749
@harrietread1749 3 года назад
thank you
@maneshwarsingh8688
@maneshwarsingh8688 4 года назад
WHERE IS THE "CODE IN THE DESCRIPTION"? Looks like I'm never able to see it
@BackToBackSWE
@BackToBackSWE 4 года назад
The repository is deprecated - we only maintain backtobackswe.com now.
@maneshwarsingh8688
@maneshwarsingh8688 4 года назад
@@BackToBackSWE I'd really appreciate if you give the codes to people who really need it for free. Great job explaining all the tough algos 🙏🏽
@dhananjayan281
@dhananjayan281 4 года назад
You are great man♥️
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@ameensherif
@ameensherif 2 года назад
Where is the code for this, I couldn't find it in description.
@BackToBackSWE
@BackToBackSWE 2 года назад
We maintain the code on backtobackswe.com
@maayan182
@maayan182 5 лет назад
your videos are awesome. thank you :)
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@smexyapman
@smexyapman 3 года назад
you are awesome ty
@pedrolacerda2708
@pedrolacerda2708 4 года назад
Great video!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@nehapai8177
@nehapai8177 4 года назад
where is the code?
@BackToBackSWE
@BackToBackSWE 4 года назад
The repository is deprecated - we only maintain backtobackswe.com now.
@bekaryskuralbay3751
@bekaryskuralbay3751 5 лет назад
Amazing!
@BackToBackSWE
@BackToBackSWE 5 лет назад
ye
@ElGalloUltimo
@ElGalloUltimo 5 лет назад
Boom. 256th like. On to the next byte of likes. I feel special 😄
@BackToBackSWE
@BackToBackSWE 5 лет назад
nice
@swapnik1000
@swapnik1000 2 года назад
There is no code in the description 8:09
@yitingg7942
@yitingg7942 4 года назад
I don't ever find the code anymore, which makes it hard for me to write it myself even if I understand the explanation.
@BackToBackSWE
@BackToBackSWE 4 года назад
noted - we only maintain code on backtobackswe.com now
@alirezaRahmanikhalili
@alirezaRahmanikhalili 5 лет назад
good job man
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@andrewkuhlman1665
@andrewkuhlman1665 Год назад
I imagine we feel the same way about "conclusions" lol
@dhargakolla8982
@dhargakolla8982 3 года назад
Is the code moved out from this description
@airysm
@airysm 5 лет назад
Hey, I was wondering if you’re ever going to go back to the format of also explaining the code in the video kinda like the N Queens video? If not then it’s cool lol Also I’m watching this video with earphones and I just wanted to give a heads up that for the audio you could hear a low echo and also a low static noice as well from some feedback the mic is picking up
@BackToBackSWE
@BackToBackSWE 5 лет назад
Yeah, I don't think I am ever since I set the GitHub up. And also, yes I know about the static, I messed up the levels. I am still learning how to use the microphone. For this video I didn't get the mic close enough to my mouth (it was mounted to the camera). I was lazy and didn't use my boom kit. Check out today's video, I made the sound better I think.
@Salesforce_Nexus
@Salesforce_Nexus 4 года назад
awsome!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@danielebanana
@danielebanana 2 года назад
Where is the code? I can't find in the description
@BackToBackSWE
@BackToBackSWE 2 года назад
You can check out the code repository on backtobackswe.com/
@mohammad-aminebanaei886
@mohammad-aminebanaei886 2 года назад
Awesome
@ViktorTorma
@ViktorTorma 4 года назад
this is awesome :D
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@mohammedcardian
@mohammedcardian Год назад
where's the code ?!
@BackToBackSWE
@BackToBackSWE Год назад
Hi, the code is maintained on www.backtobackswe.com
@tobioy
@tobioy 4 года назад
uh, where's the code
@BackToBackSWE
@BackToBackSWE 4 года назад
The repository is deprecated and we only maintain backtobackswe.com now.
@techzoo1
@techzoo1 3 года назад
I feel like also adding and talking about code rather than just talking about algo would be helpful.
@digvijayyamagekar4300
@digvijayyamagekar4300 2 года назад
where is the code man ?
@oborofolusho212
@oborofolusho212 5 лет назад
I neeeeed serious help with compiler, do you guys do that!!!!
@BackToBackSWE
@BackToBackSWE 5 лет назад
haha I'm not a compiler expert
@markohy9samy565
@markohy9samy565 Год назад
dfds
Далее
How Binary Search Makes Computers Much, Much Faster
6:51
Is Computer Science still worth it?
20:08
Просмотров 363 тыс.
Intro to Binary Trees and Breadth First Traversal
21:40
Binary tree: Level Order Traversal
11:23
Просмотров 607 тыс.