 What's up guys, Mike the Coda here. Today we're gonna go over another lead code problem. This one is going to be finding leaves of a binary tree. So you're given the root of a binary tree and they want you to collect the tree nodes doing this, which is collect all the leaf nodes, remove all of them and then repeat this until the tree is empty. So if you look at this, if you actually look at this example here, let me just quickly paste this. Here, if you look at this example here, what's going on is that we have this tree of one, two, four, five, three and they're gonna just remove all the leaves of this tree. So they're gonna move four, move five, move three and then in the end they get one and two. Then we're just gonna repeat this process until our tree is empty. So we're gonna remove the leaf, so we're gonna move two and then in the end we have one and then we're gonna move one and then the tree is empty. And each time you remove a leaf, they want us to just add that into an array. So yeah, given the root collect all the tree nodes by doing this, collect all the leaf nodes, remove them and then repeat until the tree is empty. So yeah, so you have one, two, three, four, five, we remove four, five, three and then we move two and then we move one. So then the array in the end will be have four, five, three, two and then one. Likewise, if we just have the root one, it's just gonna output an array of one because we remove just one, that'll just be an array of itself. Okay, so how do you do this problem? Excuse me, simply it's just to, you just have to store in each of the nodes with its corresponding height around the leaves with its corresponding height. So what we're gonna do is we're just gonna go through each of the heights and for each of the heights, we're just gonna store the current leaf for that and then yeah, and then in the end, we're just gonna return our array solution in. So here I'll just show you guys what the code of what I mean. So here we have get height, which is just like, actually let's start for the top. So we have vector vector int solution and this is just like a vector vector int. It represents a two-dimensional array. So they want us to have a two-dimensional array here because they want us to return a two-dimensional array with each of the leaves that are removed and each succession. I don't know why that did that, okay. So yeah, so what we're gonna do essentially is that we're going to use recursion, recursively call it and any time we get to the current height, we're just gonna like add a new empty array and in the end, we're just gonna just keep pushing each of the current nodes onto the current height and then yeah, we just return solution in the end. So here I'll show you guys what I mean. Let's actually just trace through this code because it's much easier to understand it if you know what the code is doing if you actually trace through it, okay. So here at the top part, I have vector vector int solution which is just a global variable representing the solution of all the nodes that we're gonna collect while we're removing them. So yeah, and then in the find leaves method, we just call recursively call get height passing in our root and then we return the solution that it collects from this method of collecting all the leaf nodes and then removing them and going to next step. So let's actually trace through each of these through each of the recursive steps of get height and then we'll be able to understand what we're doing, okay. So first of all, root right now is one, right? Because here root is the first one, first of our tree is one, right? This is the root, right? We're starting, the starting node is root, okay, here's root. So we pass in root into our get height function, okay? So in get height is the root null. No, it's not, right? Because first root is one, it's not null. So what do we do? We call get height again, but we're gonna pass in the left side. So here, if we were to go up to here, we're gonna call get height and we're gonna pass the left side. So this is get height, get height and we're gonna pass in the left side. So what is the left side? Roots left is two, right? Based on this picture, one, right? The one is a root, the left side is two. So we're gonna pass in two. So we're gonna pass in two here, okay? And then what we're also gonna do is we're going to pass, yeah. And then pass in two. The right side, get height H, right? For the right side, we're gonna pass in the right side. So that's gonna be three. So we're gonna get height three. So here we have two function calls that we just had. So here, I'm gonna just make sure you guys understand. So we have two function calls after passing in. So here's what happened. So we had get height passing in root. Get height root was one. So we pass in it and now we have two function calls. I'm just gonna show you guys this diagram so you guys can understand. So this is what the diagram is, okay? Get height one and get height three, okay? So this is left side, get height passing in two here. Left side and get height three is the right side, okay? Now, yeah. So for now, those values are gonna return some certain height and then we're gonna use that for the next value, but for now, that's what happens. Okay, so now let's just go through get height two. So in get height two, so we pass in this time two into our function. So it's two null, it's not, right? Two is not null, so we're not returning negative one. And what does it do? Now what it's gonna do, it's gonna call get height passing in the left side of two, which is gonna be four. And then we're gonna call get height passing in the right side which is gonna be five. So here, after this function call, this is actually gonna call, let me just minimize this real quick because this is too big. So let's actually make this smaller, okay? Whoa, now it's kind of pixelated. But yeah, here it's gonna pass in get height two, get height, so this is get height two. It's gonna call get height four and then get height five. Just get height four and then it's gonna pass in get height five, okay? All right, and then those are gonna return something and then they'll do something later after that. So now we're gonna just look at get height four. So what does get height four do? Get height four is gonna come up to here and get height four is not null. So what it's gonna do is it's gonna pass in get height, four is left is gonna be null and four is right is also gonna be null. So it's gonna do get height null. So here four is gonna pass in get height null and then get height null, okay? And then get height null, what does that do? So get height null is null, right? So they're gonna both return negative one. So here, both of these are gonna return negative one. So this is negative one, negative one is returned, negative one is returned here, okay? So when negative one is returned, it goes to this part of the line of code. So get current height, what is current height? What is the max of negative one and negative one? That's just negative one. And then negative one plus one is gonna be zero. So this whole thing gets returned with zero. So the current height here is gonna be zero. So this is gonna end up giving us zero. Current height is gonna equal to zero, right? Current height is gonna be zero, okay? Now, is the solution size equal to current height? Yes it is, right? So the current solution size right now, our solution size is a two-dimensional vector, right? They're both empty. It's like completely empty. So is the solution size equal to zero? Yes it is. So what we're gonna do is we're gonna push an empty array. So here, our solution size is now gonna get pushed an empty array. So it's gonna be like that, okay? Now what it's gonna do is that we're gonna use our current height of zero. Current height is zero. So at zero, solution zero, which is the first empty array, we're gonna push the current root. So what is the current root here? Current root is four, right? Current root is four because we went to the left side all the way down here and our current root is four, right? That's what this part of what we were doing. So it's gonna push four onto this. So this is that's, so it's gonna have four here now. Okay, and then what it's gonna do, what is it gonna do? It's gonna return current height. And the current height is zero, right? Remember, current height is zero in the calculation. So it's gonna return current height. So this whole thing of get height four is gonna return current height of zero. So I'm gonna actually remove this now. So get height four is gonna now have current height is zero. So let's actually just get rid of this so it's less confusing. So get height four is now gonna have, get height four gives us zero. Gives us zero, right? Current height zero. All right, so now we had get height four, right? Now we have to get height five, right? So what is get height five? So get height five is gonna call, recursively called get height on the left side, which is null, right? So five's left side here is null, right? Five's left side here is null and five's right side is null. So we're gonna pass and get height null for both of them. So here is gonna split into get height null. I'll just say get H null and get H null, right? Representing the left side and the right side. So yeah, so now since it's null, right? It's gonna return negative one. So both of these are gonna return negative one. Okay, so get height null and get height null on the left side and right side where we return negative one. So then what happens is that both of these are gonna return negative one. So what is the current height? So here current height is gonna equal to max of negative one, negative one and max of negative one, negative one is gonna be negative one, right? And negative one plus one will give us zero. So current height is gonna equal to zero. So both of these is gonna give us current height equal to zero. So this is gonna give us, let me just make this bigger. This gives us current height is gonna equal to zero, okay? So now is the solution size equal to current height? It's not, right? Our solution size has a size of one currently, right? With an array of four. So what are we gonna do? Solution at current height of zero is gonna push back the root's value. So since we recursively called on this get height of five, our root currently is gonna be five. So what we're just gonna do is it's gonna solution at current height of zero is gonna push back five. So this array at current height of zero is gonna have out of five here. So it's gonna append five here. So I'm gonna remove this. I'm gonna remove this and out of five, okay? Because that's what this function to get height did, right? The root's current value would be five because of get height of five, okay? So after we do that, we're gonna return current height. So this whole thing is gonna return current height of zero. So this whole thing returns current height of zero. So let's actually get rid of all this. Current height of five is gonna return, get height of five is gonna return zero. So here, zero, okay? Get height of five is gonna return zero. So now we have the left side of zero and zero. Okay, so here we have left side of get height four gives us zero, right side of get height two, five gives us zero also. This is kind of hard to read. This is get height two. I hope you guys remember get height two and just get height three, okay? But yeah, okay, now that we have this, we have left side of zero and right side zero. So here, what is the current height? What's the max of zero and zero? It's zero, so we plus one. This is current height is gonna equal to one now. So with both of these current height is equal to one. Current height equals to one, okay? Now is the solution size equal to current height? So our current solution size is just this one array, right? It's just this one thing, right? So yes it is. So what we're gonna do is we're actually gonna push back an empty array, right? Because our current height, our current solution size is just like one dimensional array and our current height is one. So we're gonna push back an empty array. So this is gonna have another empty array now. So now we have two arrays inside our solution, one with four, five and another one is empty array. Okay, now what it's gonna do is we're gonna do solution at current height, which is one. So solution at current height, which is like the newest array that we add, which we have, and then we're gonna push back the roots value. So what is our current roots value? Our current root value is two, right? Because this function called two called four and five. So this our current height here, our current root is two. So what it's gonna do is it's going to get the current height of this of one. We're gonna push add two. So this is gonna be two in here. So I hope you guys understand what we're doing. It's gonna have two in here. Push back two. And then we're gonna return current height. So then current height, this whole thing of get height two returns current height of one. So this whole thing of get height two is gonna return current height of one. So I'm actually gonna remove this and this. So let's just redraw this. I know it's kind of pixelated, but does get height two returns current height of one. So current height of one. So the number one, that's what it returns. Okay, so that's the left side of the left H. Now we have to look at the right side. So this is get height of three. So yeah, I know this is kind of hard to read. So I'm actually gonna delete this again and just rewrite get height of three. Because that's kind of hard to read. Okay, so this is get height of three. Okay, so get height of three. What does this do? So get height of three. It's gonna recursively call get height on the left side and the right side. So three is left here. There is null, right? Because the left side, there's nothing there. And the right side is also nothing there. So it's gonna call get height on null. So this is gonna return get height, call get height on null. Get height on null, okay? And get height of null, what is that gonna do? Okay, so if we pass a null and the left and the right side, since the roots here values are now null, it's gonna return negative one. So here, both of these are gonna return negative one, negative one, negative one. And the current height that we're gonna get is the max of negative one, negative one plus one. So negative one, the max of negative one and negative one is gonna be negative one. And we add one, current height is gonna be zero. So this is gonna return, this is gonna have curr height is gonna be zero. Okay? So is the solution size equal to current height of zero? No, it's not, right? The solution size has a 2D array or a two-dimensional array with a size of two, right? So it's not equal to zero. So we're not gonna add a new array. But now what we're gonna do is we're gonna get solution at current height. So the current height is zero, right? And then what we're gonna do is we're going to push back the roots value. Okay, so solution at current height of zero is gonna push back roots value. So what is the current height? Current height is zero. So the first array, the first array that we look at in our solution array, we're gonna push back the roots value. So our current root is three because of the recursive calls on left side and the right side of get height of three. So what it's gonna do, it's gonna look at the current height of the first array here at current height of zero. And we're gonna add three to the first array. So here, the first array here at four or five, we're gonna add another value here and we're gonna push it and it's gonna be three. So here, now we add three here, okay? So I hope you guys understand what we're doing. Essentially what we're doing is we're gonna go through each of the heights and we're just gonna push corresponding values to it at each of the certain heights that we are at, okay? If that makes any sense. So like every time we go through like a surrounding leaf node, we're gonna push those values into one array and then the next leaf nodes, we're gonna push those values into the next array and so on and so forth, okay? That's essentially what we're doing. All right, so after this of pushing three onto the current height here, we're gonna return current height. So what is current height? It's zero, so it's gonna return zero. So get height of three is going to return zero. So here, this for get height of three, let's actually remove all both of this stuff, all these values. Current height, it's gonna return current height of zero. So here, let's remove this also because now it returns current height of zero. So current height is gonna equal to zero, okay? So now we have current height of one and current height is zero from the get height of two and three. So get height of two and three, now have those. So what is the current height now? So what is a max of height one and zero, right? Left side of one and the right side is zero. Well, that's one, right? Max of one and zero is one. So then now we do current, yeah, current height is gonna equal to one plus one. So that's gonna return, both of these are gonna return one plus one is gonna give us two. So this is current height is two, okay? And yeah, so now is the solution size equal to current height of two? Yes, it is because there's two arrays in our solution. So what it's gonna do now is it's going to push back an empty array onto it. So now we're gonna push back an empty array of this, which is just an empty array here. And yeah, now it's pushed back an empty array here. Now what we're gonna do is we're gonna do solution at current height of two. So that's gonna be the new array that we just added since our current height is equal to two. So that'll be zero, one, two, right? The new array that we added. And we're gonna push back whatever our root is. So since our root was now one, because it's the recursive call of starting of one after all these return statements, it's we're gonna push one onto this new array here. Bam, okay, so now that's there. And then after that, we're gonna return current height, which is gonna be two. Okay, so that this current height is gonna get returned. And then this whole thing gets returned, get height of one would get returned of height two. So we're gonna go here, move all this, and it's gonna return current height of two, current height of two, okay? That's what get height of one does. So now after this whole thing gets returned, we don't actually need that, the height value, so we're just gonna ignore it. And since we already added all the values into our solution array here, we're just going to return solution. So this gives us our answer in the end, okay? So I hope you guys understand what's going on. Essentially what we're doing is that we're going to, every time you go through a new leaf, we're gonna add those values based on the corresponding height into its corresponding yeah, based on this corresponding height, add it into our solution array, okay? So whichever height that has starting from whichever node it is, right? We're gonna have that be added into its corresponding array. So essentially what it's doing is like, we look at the leaves here, four, five, three. Since both of all these values have a height of zero because if we look at this, the root here, there's no, there are nothing there, right? There's like leaves there or have like, there's no, there's no children of each of these four, three, five and three. So what it's doing is that once it gets the heights of each of these values of the leaf nodes, it's gonna add it to its corresponding array inside our solution, 2D solution array. So at that certain index of that certain height of two of no children height, right? We're gonna just add that into that array. So that's why those leaves gets added of four, five and three gets added into the first of size zero. And then since if we look at two's height here, two's height has a height of one because there's no, because there's children of two. So in our solution array, if we put two as, we put the number two into the solution of height into the solution array of at height one, okay? And then for one, we put one's solution, the node one into the solution array at height two, okay? Because of that's, because from the starting positions. So yeah, in the end our solution is gonna have the exact corresponding values at the right location based on each of the nodes height, okay? We're basically storing each of the nodes in its corresponding height in our solution array. That's what we're doing if that makes any sense. So yeah, yeah. So that's just tracing through all this whole code. I hope you guys understand what I'm doing here. I know it's kind of confusing, but it is, what we're doing is we're just going through each of the leaves and putting them into the corresponding new array and then based on their heights. And then we go to the next one and put it into the corresponding array. But yeah, I hope you guys enjoyed this video. Rate, comment, subscribe. I'll check you guys later. Peace.