Binary Tree Level Order Traversal

preview_player
Показать описание

DISCORD CHANNEL
----------------------------------------------------------------------------------------------------------------
In this Discord channel, you will be able to...
1. Ask me questions directly (as well as other members)
2. Ask about and discuss previous interview experiences
3. Find mock interview partners
4. Share helpful videos for interview preparation, and more!

This question is commonly asked by the following companies: Google, Facebook, Amazon, Uber, Microsoft, Oracle, Bloomberg, LinkedIn, and Walmart Labs.

Intuition behind solution: Create a queue and add the root to the queue. While the queue is not empty process all the nodes in the queue. At every iteration of the queue not being empty, get the size of the queue (this represents however many nodes are on the current level of the tree). Iterate through all these nodes with a for loop, adding their values to a "current level" list. After adding their value to the list, check if they have left and right children adding them to the queue is they do exist (this allows us to process the next level of the tree on the next iteration of our while loop). Once our for loop terminates we have populated a list of all the nodes' values on the current level and we add this list to our return value (a list of lists). Once our while loop ends we have processed all the levels of the true and therefore return our result (our list of lists).

SOCIAL
----------------------------------------------------------------------------------------------------------------

MUSIC
----------------------------------------------------------------------------------------------------------------
Blushes by Dj Quads
rose & boujee by memori ("me over mori")

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

came for the BFS advice, stayed for the chill lofi migos remix

jeremyvan
Автор

I am glad I saw your link in the leetcode discussion. I need to practice more queue and stack "iterative recursive" algorithms.

dephcn
Автор

Such a simple and genius explanation! Before this video BFS for me was harder than recursive DFS. Now I can understand both. Big thanks to you!

readonlylogin
Автор

This is amazing. I struggled understanding BFS but this is the best explanation I have seen so far. Thank you so much!

soniagrzywocz
Автор

Very helpful video, I was struggling with the explanation on leetcode. This video made it too easier to understand and that too in minimum amount of time. Thank you!

alankardingshsingh
Автор

You're very awesome. I always search for your videos first whenever I get stuck in a leetcode problem. Thanks a lot

zaidaldhahi
Автор

That Walmart Labs gesture was hilarious though! Please upload some videos for Linked Lists as well.

shubhammittalSHM
Автор

always coming thru w a clear and concise explanation/approach.good looks bro keep it up!

stanleerubio
Автор

Awesome man! BFS within just 5 minutes with such clear explanation

RomanReigns-tgqm
Автор

Thank you, very much, i was stuck since it was hard to keep track of levels

xxpirategamingxx
Автор

The "int size = queue.size()" was the trick and I was missing it :). Great

punerealestatebuilder
Автор

My man has exquisite music taste. I love it lol

jamesvick
Автор

Have been looking for the level-specific control over the BFS for a while. The trick found in this video is to iterate the level with the queue length.
This trick is also useful to find the level-average of a tree (another FB question on Leet) Thanks man!!! also watched the running gurlfriend behind you in other videos :-) lol..

SreeOne
Автор

Wow...so simple & superb explanation.

Spider-Man_
Автор

Short, easy, precise explanation. Thanks.

TheSantouryou
Автор

@Kevin Naughton Jr.
Before watching your video, I implemented a level-order traversal that was okay, but it put everything in just one list.
But after watching your video I realised that I don't have to use just one dequeue operation for each enqueue operation.
Like a lightning that struck me.
And for size of current level, I was using 2^n kind of logic. but your answer simplified it very much.
Thank you very much!!!

maheshdudhanale
Автор

You are right. I always found it difficult to understand BFS and wound never forget it now. Thanks

HarpreetSingh-eskq
Автор

Yoo I am so thankful for your channel, I am able to learn a lot thanks to your easy explanations! Thank you so much!!

mythilikarra
Автор

Man! I love your videos. Super cool explanation. I would love it if the background music was a bit less loud if that is okay by you!

darshansimha
Автор

Thank you thank you thank you. Such a simple and elegant solution!

cakec