filmov
tv
Binary Tree Level Order Traversal
Показать описание
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")
Комментарии