Hook

tarabyte tech youtube channel
https://www.youtube.com/channel/UCw0w0_3f5h_b_0q1_n_0_1g ↗Their other posts in the index, biggest breakout first.
solve a leetcode with me I failed this interview question when I was in college let's solve it together hey I'm Tara I'm a senior software engineer and I used to teach computer science at Carnegie Mellon Given a binary tree root, return the level order traversal of it as a nested list, where each sublist contains the values of nodes at a particular level in the tree, from left to right. Example 1: Input: root = [3,9,20,null,null,15,7] Output: [[1],[2,3],[4,5,6,7]] Example 2: Input: root = [1] Output: [[1]] Binary Trees I failed this interview question when I was in college and so I totally blanked and bombed it and did I cry in the bathroom after yes I did it's redemption time let's solve this so there are many ways that you can approach this but to me this is a classic use of breadth first search because breadth first search basically traverses the nodes of the tree in level order anyway and so we might as well use it let's talk through the algorithm here so I'm gonna use a queue and a queue is a first in first out data structure and you'll see why that's really effective here that means when you add an element to the queue you'll also read that element first I'm actually gonna initialize our queue with the root node which happens to be this one node right here I'm gonna perform the same algorithm on each level so for each level I basically want to look and see how many things are in my queue because that's like how many nodes are on this level 1. get size of level 2. for each node - process node - handle the children so once I get the size of the level basically for each node in that level I basically want to process that node and then I want to like handle the children the unruly children so what does that mean well let's walk through this example so first we have this one right here and we want to get the size of the level which is just one so first we need to process the node which is just one so first we need to process the node well what does that mean it means two things in this case first we need to remove it from the queue so we're gonna pop that here and we actually gonna add it to this thing that I've named level list and level list is just gonna keep track of all of the notes that are in that particular level but now we need to handle the children and its children are its left node and its right node so we're actually going to just add those to the queue so I'll put two and three in our queue and then whenever we get to the end of each level so we're gonna add this whole level list in here to the results we've handled the first level of our queue which was just this rude guy right here so now we're actually gonna look at this level okay so we're gonna get the size of the level which is two so we're gonna process the node and handle the children so let's process this two node well that means that we need to dequeue it so we're going to remove it from the queue and we're going to add it to the level list and now we need to handle two's children well two's children are four and five so we're just going to add these to the queue our size was two we just handled the first element but now we need to handle the second element which is this three so we need to process the node so remove it add it to the level list so we need to handle the children which means we need to add them to the queue so I will add 4, 5, 6, 7 so we just finished this first level and we are level list is 2 3 so we're gonna append this into our results now we are on the next level so we need to get the size of it this one is four so basically for each of these we're going to process the node and handle the children so we'll process four which just means we'll dequeue it and we will add it to the level list but none of these have children right these are on the bottom level here are all childless we can just add them all into the level list because that's really the only processing we have to do for them so we will dequeue five add it to the level list we'll dequeue six add it to the level list and we'll dequeue seven add it to the level list and then we're gonna just add the level list into our results how do we know we're done? well our queue is empty so we have no more work to do we've traversed our entire tree in a breadth first search fashion using a queue and this is actually the right answer if you want to see me write the code for this I have it up on my YouTube thank you for following along