 What's up guys, Mike the coder here today. We are going to go over the next problem of Coding interview patterns. So the next problems of the next patterns that we see most in coding interviews is Breath first search binary tree traversal, okay So this is essentially the same thing as level order traversal for binary search trees So the idea remember in graphs for binary search trees for graphs, right when you do a Breath first search traversal you just go over the surrounding nodes first And then you go to its surrounding nodes and so on and so forth until You visited all the nodes, right? Well since for binary search trees, it is the exact same thing But it's easier because we only have two children for every node, right? So when you do a breath first search on a binary search tree, you're really just going over every single level So let's say we have this one one two three four five, right? So this this binary search tree right here. Well, if I'm going breath first search here We're really what we're really doing is we're going through every single level So we're going to go to one then we're going to go to two three right So each level here from left to right from every level so one ones level is just by itself, right? There's no nodes from left to right. So it's just one Then we go to the next level right here, which is two and three So that's that's the next level. So two three and then the next level would be four and five right four five And that would be the last level right there now if there were more nodes here Let's say there was six and seven we would print out four five six and seven because that's The whole level of the binary search tree and as we could see here on geeks for geeks The output would be one two three and then four five. So there's many ways to implement this Um, oh, yeah, we could go over another example here actually So look at this 20 it would just be level order. So we 20 then 822, right? So we've got first level 20 822, and then we have four then 12, right? And then we have 10 and 14 so each level so as we we're going levels So we're going 20 822 or 12 10 14 right every level from left to right. That's what we're doing Okay, so there's many ways to do this one way is to use recursion And I find recursion more difficult here because you're essentially using Run depth first search while you're doing doing breath first search basically and you're going to use recursion and Maintain the current height and then every you're going to print nodes for every height from the root and match if the current height is equal to the Height of the current iteration and then print the data. So this is kind of very hard Till grasp, but I'll bear with me, right? Basically what we're going to do is going to loop from the current height to the height of the tree So when one age and then we're going to run depth first search, right? So basically we're going to keep going down all the way down to every single nodes We're going to pick a note and we're going to go all the way down all the way down to that specific node While we're maintaining the height of the current node If it's no, we're just going to return right if the level is one So once we reach one we just print the data. Otherwise If the level is greater than one we recursively call on the left side and then subtract the level minus one and Recursively call on the right side with level minus one. So this is kind of hard to understand but bear with me, right? So here's how you do it. Okay, so We have a level order right so here we have a node called print level order Okay, which is a Function to print the level order of it. This is our breath first search function, right? And then we're gonna first we have to get the height of the tree of the root Which is you have to call another function to do that, which is a pain in the butt. So here we're basically Keep going down and get the height of the binary search tree over and over again until we get the height of the tree right so this is this is kind of hard to understand but Getting the height of the tree is just literally you just keep going down every single time from the left side and the right side And every time you go down We're gonna increment one on the height to the left side and the right side, okay? That's that's what getting the height of a binary search tree is like, okay? So yeah, if the left height is greater than the right side, right? We just add one. Otherwise We we add one on the right height, right? So that's that's all we do here Yeah, they didn't yeah, okay. That's basically and we hit the root. We're just going to turn zero So this is like getting the height of the tree So once we get the height of the tree We're gonna loop through from one to the height of the tree and we're just gonna print every single level From starting from the roots and I okay, so I is like the current level so Here we have another function called print current level and what we're doing is that we're just going to recursively Decrease the level over and over again until we reach a level of one And then that means we print the roots data. Okay, so if I were to draw this out This is kind of a pain in the butt, right? So the recursive option is really difficult to implement I personally wouldn't even use this actually, but I feel like it's good to know because if you understand the recursive process It's just easier to It's just good to know it's good to know in case of like the interviewer just wants you to do a recursive process and a Inter iterative process So let me just quickly get my stuff in there So guys going back to the height of the tree. So if we go back to right here the code of the height getting the height of the binary search tree Right is just this one right here Right, so it's just going getting the left side and the right side and then whichever height is larger They are going to add one and recursively call this so basically what they're doing is that we're over here and Basically the height starts at zero right actually there's no height in the beginning But we're gonna recursively call on the left side. So we're gonna go this side, right? This side left side and then we're gonna go where first we call again. So we're gonna go left side again Here and then once we're here right we're going to call again, and then we're gonna hit null So that's gonna return zero on the left and then it's also gonna return zero on the right And then we get the maximum whichever side is max We're gonna add one to it. So this which is a lot larger left side is larger than right Right side is large enough. Otherwise, we're gonna add one to the right side return one So this is gonna plus one for one height, right? backwards So it's gonna plus one for height for this side and Then we're gonna recursively call the right side and it goes to left child again So we go to the right side and then we're gonna go on the left side here. It's gonna return zero on left There's nothing zero on the right whichever is maximum. It's gonna plus one. So zero plus one will be plus one here And then it goes on the right side the person on the right side right side zeroes and all right so return zero and Right side is gonna be zero also. So return zero and On the left side Whichever the larger on the left and the right will be zero max is left of the right zero zero and then plus one There's one here and then we take the maximum on the left child here There's plus one and the maximum right child here plus one So whichever is larger since there's left side is not large on the right side We're gonna go to this return statement. So we're gonna do right side plus one. So Plus one plus one would be two. So this is gonna come up to here And then now we're gonna check again, whichever is larger on left and right. So left is not larger than right So then what we're gonna do is we're gonna return right plus one So two plus one would be three so that's be here and that's the left side Since we're out down there and now we're gonna recursively call the right side So we got the left pipe now We're gonna first upon the right side around the right side here left is nulls return zero right is no return zero We're gonna plus one here. That's one here. So now we have left three and one and whichever is larger Three is larger than one. So we have that and then we're just gonna add one. So left side three plus one would be four So in the end We are height is four. So that's that's just going through Getting the height of the binary search tree. So now we have the height of the binary search tree now We have to go level order traversal. So now this is the time where we start looping through So now that we got the height of the binary search tree That's this part of printing level order. So that's this part now We're gonna loop from one to the height. So remember the height here is four So we have four and we're gonna loop from one to four. Okay, we'll look from one to four. All right, so We're gonna pass in one here Okay, and then If the level is one, we're gonna print out the roots data So in this case the first level is one, right? So that's 20. So Since the level is equal to one, we're just gonna return 20 so here There's gonna be 20 here. So we print out that now I is gonna equal to two right because we start at one Now I is gonna equal to two. So now I is equal to two. We're gonna call level here with a two minus one On the left side and we're gonna call level on root Two minus one on the right side. So now it's gonna do go on the left side. So this is the left side Right, we're gonna go on the left side Two minus one would be one. So we go here root is equal to null But it's not equal to null right level is equal to one. So we're gonna return We're gonna print out this roots data. So this roots data is eight. So we're gonna print out eight Okay Now we're all done with the left side. We're gonna go on the right side. So we're gonna go on the right side here and We're at 22 right 22 So now we pass in the level our current level I is equal to two So we're gonna two minus one would be one so that that comes back to the top now if level is equal to one We're print out the data. So our current data at this side on the right side is 22 So we're gonna print out 22 here. So you guys could see the what is basically going on We're basically printing out things level by level. Okay. So now we're gonna have I equals three So I was equal to one right then it became I equal to two now I is gonna equal to three So now that I was a three. We're gonna go down here So we're gonna go here. Okay. So we pass in three and In here and then we're gonna do recursively call on the left side roots left with three minus one So it's gonna be two right. So we're gonna go left side Passing to here right and then we're also gonna call right side pass in two Right because I was a three. So we pass in level minus one Three minus one is two. So we go on the left side here. So left side to Left level is still not equal to one yet. So we're gonna go down here and we're gonna pass in left print current level left on The current level so our current level is two right now because we just called it so we're gonna do two We're gonna call level left two minus one, which will be one. So we're gonna call it left one and we're gonna call Right one, okay, so we're gonna call right one All right. So now we down to a level of one. Okay, so now we're back up to here We went left again one and because level is equal to one We're just gonna print the data, which is just four. So we print out four here. So that's this part, okay? And now that's done. We'll go back to here. So when we're on the right side of one So one gets passed up here level is equal to one now So we're gonna print the current data and our current data is 12. So we're gonna print 12 here So I hope you guys are seeing what's going on here Seeing what is going on with each of these level traversals, okay? All right. Yeah, and I think that's that so we have now 28, 22, 4, and 12 Okay, so now i's are gonna equal to 4 so Right remember we looped from one to four so now we're finally on i equal to 4 so Since i equals to 4 now, we're gonna get rid of this side. So let's get rid of this Let's get rid of this Let's get rid of this So i's equal to 4 and 4 goes back up here. So we're gonna call 4 is greater than 1 right? So we're gonna go on the left side with a level minus 1 so 4 minus 1 is 3 so we're gonna call left side 4 minus 1 is 3 and then we're gonna call right side 4 minus 1 which is 3 so now we're on this side. Okay, so So we're calling left on 3 so we're going to go back to the top so print current level left on 3 Now that we're at 3 3 is still greater than 1 right? We're not at 1 yet. So 3 is greater than 1 so we're gonna recursively call on the left side on level 3 minus 1 so right now it's 3 so we're gonna call left on 2 and Then we're gonna call right on 2 okay, so now you see that So now we're left on 2 right on 2 okay So now we go back up to here and then Left is gonna be we're printing current level left 2 so 2 is still greater than 1 so we're gonna call level 2 minus 1 So this is gonna be 2 minus 1 which is 1 and then right would be 2 minus 1 which is 1 okay, so we're level 1 and level right 1 so now We're at 1 we're just gonna print the data which is 4. No actually hold up. I think I'm Did I miss miscalculate this? Went to the left one for oh, okay left one. Okay, so now we're at left one right um now Left is one, but I think when we're going down left again There's nothing here this level has nothing here, right cuz we went okay. We went left Okay, I'll show it this way we went left to one and we went right one It's much easier to see and think about So we went left one right one So we went left one right one and now we go back to the top and now route is equal to null because there's no nodes on here So we just return so left one is null So there's do nothing right one is no so we do nothing so here at this point It just returns so let's just like get rid of this so this is like gone Okay, so that was left to now. We're gonna go right to right so we're now right to two is greater than one so we're gonna pass in Level two minus one so it would be One so left two minus one would be one and then right two is gonna be passing here And then love those two so two minus one would be one so here right one So now we have left one and right one Um, yeah, so now left one one right so now we go back to the top level is equal to one So we're gonna print the current data and our current data is 10 here on the left So we're gonna print out 10 so it should be 10 bottom here 10 and then Yeah, we go back to the top we're gonna right one and Now root level is equal to one so we're gonna print the current data and their current data is 14 So we're just gonna print out 14 here. So this would be our output So I hope you guys understand what our output here is so let's just I'm just gonna make sure we've got the output here You guys see our output So we have I'm just gonna make sure you guys see this. So this is our output. We have 28 22 4 12 10 14 So that's matches exactly the level order right 20 Right 20 20 8 22 right 8 22 this level is 8 22 4 12 is this level 4 12 which is right here And we have 10 14 on the last level which is 10 14 So yeah, usually so did in different levels and our output is correct. So yeah, that's how you do it with Recursively this is a pain in the butt. Um, the easier way to do this is actually with iteratively Okay, so that was breath first search recursively Using that first search to traverse to get the height and then using breath first or so so there Yeah, that was a that was a one hell of a trying to get a lot of order. I apologize for that That was very difficult. But yeah, well now we're gonna do that the next part which is actually doing it with Using a cue which is iteratively and it's way easier. Okay, I promise you this is way easier Okay, guys now we're gonna do this with a cue which is iteratively and this is so much easier to do Yeah, I don't know why we didn't do this earlier, but this is legitimately legitimately so easy to do Okay, so let's go back to the original picture. Let's actually just take this picture because we don't There's no need to Make this so difficult Let's take this picture and Okay, so we're at this tree, right and what we're gonna do is we're gonna call Breath first search which is using a cue and this is so much easier. You'll understand why it's so easy so much easier So if you think about a cue, it's just like a line, right? It's a line of people like a cue when you're waiting at like a grocery store. It's a line So when you're waiting at a line, whichever goes first First and first out right so you basically go in the first line and then You repeat until the cue becomes empty. So yeah, it's it's not that difficult. Let's let I'll explain how to do this Okay, so Basically, we have a cue waiting in line. We're gonna add the root onto it if the cue is not empty We're gonna get the first value of the the node that we're at and we're gonna print it out And then we're gonna pop it all right and then once we pop it We're gonna go on the left child and push this onto the cue and then we're gonna push the right value on the cue So this is this is really not that difficult. So So let's say we have this this tree right one two three four five. So right now the cue is empty. So For the cue I'll make sure it's like a line. Okay, so this is our cue or line So the cue is empty. So we're gonna first push the root which is our starting node of one So now it's on here. Okay, so that's this part We push the root onto it here now now is a cue empty. No, it's not so we're gonna get the front value So what is the front value of this tree? It's one. So we get this one. So we have the value on now Okay, and what we're gonna do is we're gonna print it. Oh Yeah, we're gonna print it and then remove it. So we have this value print it We're gonna print one remove it remove it. So now it's gone. So the one value is now gone all right now we're gonna push the left side of The cue and then we're gonna push the right side of the cue. So the left side is two and three So we're gonna add two first and then we're gonna add three Okay, so now we're at two and three now is the cue empty. It's not so what we're gonna do is we're gonna take the first value on The front so we're gonna remove two Okay, so now we're at where we moved to and we're gonna print it out So we're gonna print out to so we're print out to all right Now we were we print out to we print out to or removed it now We're gonna add cues children to children which is four and five to the end of the cue So we're gonna have four and we're gonna add five. Okay. So now now we had four and five, okay Now we're gonna get the next value of the cue which is three So remove three and then we're gonna print the value three. So three is gonna be here So we're gonna print out the value three. So now we have one and then two three which is printed out on the value there Okay, now that's it because three has no children right three has no children So we're not and we're not gonna add any of its children. All right, so now we're gonna is the cue empty It's not empty yet. So we're gonna get them the next front value of the cue which is four So we're gonna remove four. Okay, and we're moved for we're gonna print four out So we print four out and we're gonna get fours children for his children is null, right? Four fours children is empty. So there's nothing there. So we're gonna do nothing there and then yeah, we're done We're at we're done with that five. We're gonna remove five and then push five We're gonna print out five. Yeah, we're five. We're gonna print out five and print that out And now we're gonna get five children. So five children is nothing. It's empty here. So then that's it Yeah, it's up there. We can't add anything adds onto the cue. So now that's it our cue is empty So now we're done. So yeah, that's pretty much it now our output is just this Value one is one which is there which is the first level two three is the next level from two to three Right two three is the next level four five is last level four five. All right So yeah, that's that and that's the code. That's legit the code You know how we don't have to do anything that was the whole code and I hope you guys understand this why this Cue is so much easier than recursively calling it. But yeah, you might need to use recursive call to do that All right. So now that we learned how to do a level order traversal For binary search tree now, we're gonna actually do the problems. Okay. So now when I do the problems We're literally just going to Use the pattern that we've learned here and gonna implement in the problem. So it's not that difficult. Okay. Yeah Okay, so the first leak code problem of binary tree level order traversal is Exactly what I just explained. All right. I'm gonna use the iterative example because it's so much easier and you don't have to do all this stuff, right? You don't have to do all the the run that first search get the height and then loop from one to H and then Recursively calling the left side in the right side like that's really pain in the butt to me. It's pain in the butt For this one, we're just gonna use the actual iterative method and what we're gonna do is literally the same thing as what I explained before The only difference now is that they want the end result to be in an array Okay, so instead of just printing it out. It'll just be an array So for that what we're gonna do is we're gonna have a 2d vector here Which is represents to return and every time you go through the level We need to create a new array and then add the new array into to return and then in the end We're gonna return to return that's all we have to do for this binary tree level order traversal. Okay So we start out with the queue with the value here, which is just regular value. Okay This queue you could call this queue anything. I call this queue value Maybe that wasn't a good way to Call it but it's it is there it is there though, right? Then we have to return which is just the 2d vector that we're gonna return. Okay. All right, so Before we start anything if the root is not equal to null So if the roots not empty, right, we're just making sure that the tree is there, right? If it's empty, then I mean you don't have to do anything literally you don't have to do anything But yeah, if it's not if it's not no, right, so that means it has something What we're what we're gonna do is we're gonna push back a new array of just the roots value So if I have three here right, I'm just gonna create a new array called Three that has a value three and I'm just gonna push it onto the two returns So I'm gonna add this to my array of two returns So if if I were to drive out on paint it would be something like So we have a 2d vector that we're gonna return instead instead, right? So this time the first value is we're gonna push back the roots value So it's three so what we're gonna do is we're gonna create a new array three and we're just gonna add it onto this vector So we're gonna add three into here, right? Because what we're trying to do is we're trying to have for every level We're gonna add a new array of values for each level into our two or two return array That's the only difference in this problem The only difference is that they want each level to be in a separate array So that's why we're gonna for the first node. We're just gonna return It's separate array inside there and add it into the two return So that's this part Now we're gonna run our breath for a search which is very very easy here So we're gonna have our queue and we're gonna push this queue. Okay, so our queue is gonna be I don't know It's gonna be empty for now Here we're gonna push the root on so the root is three so we're gonna push three So this is this is our queue our line. We're gonna push three Okay, so we push three onto the queue and now we're gonna create a new array So we're gonna I called it each You do you don't have to call anything I just called it each like each level you call a level will be each level here All right. Now we're gonna pop the first value of in from our queue. So that's this one We're gonna pop three out so we pop three out and what we're gonna do is going to push We're gonna push the left whatever threes left child into our queue and the right child into our queue So threes left child is nine, right? So by the way, we have to check if it's not null, right? Because if it's null, it'll just be empty So these last child is nine, so we're gonna push nine onto our queue So if you push that and then um threes right child is 20, so we're gonna push that onto the queue So push that okay, so now we have nine and 20 and while that we're gonna add it into our new array Which is just gonna be our new array here. This is our new array and we're gonna add nine and 20 in through here Okay Now we're done with that is our new array that we're adding levels into our to return So this is our this is our to return right our 2d array Is this empty of nope? So we're what we're gonna do is we're gonna add it into our 2d vector So that'll be there. So we add it to there. I'll be there and now we're just gonna Read this so that's gonna be an hour 2d vector. So that's nine and 20 and now yeah We go back to the top. We're gonna pop nine out Okay, pop nine out and we're gonna get nines Left and right child so nines left and right child is empty here. So it's null So left is null and right is null so we do nothing right because if we if there's nothing there We don't really need to add anything right For that since it's equal to null we do nothing here. So that's that and now we're gonna Go back to the top right because our each array. This is our new array for our level is empty here So we're gonna go back to the top. So now we're at 20, right? We we're gonna pop 20 the next value in our queue and pop it out and then we're gonna add its children So what's 20s children 20s left child is 15 right and set right child seven So we're gonna add 15 and 7 into our next level. So that'll be that okay, and then what we're gonna do is We're gonna check We got a pushback 15s Child here and 15s children is null on the left side and 15s children on the right side is null also And also 7s left child is null and 7s right child is null So that's that There's that stat also. We don't add it into our queue. So there's there Now what we're gonna do is we're gonna take this 15 7 and because it's not know we're gonna put it here and that's it. That's all we have to do Easy right and now since our queue is empty all the values are gone That's it and we just print it out and we return it So we return to return and we get our output of three Three nine twenty fifteen and seven. So yeah, that's all we have to do and that's the first value of this problem So I hope you guys enjoy this and we're gonna go to the next problem All right guys, we're gonna go over another problem using the exact same level order traversal But this time it's gonna be zigzag so for zigzag It's very very similar of doing the same level this time We're gonna alternate going from left to right and then right to left so here the first value We're gonna go from left to right so we're gonna have three and that's gonna be in our value of three here Then we're gonna go right to left. So we're gonna print out 20 and then nine So that's right 20 and nine. That's the next value here And then we're gonna left to right. We're just gonna be 15 and 7 so we're gonna print 15 and 7 put it there So that then we have that's it. So we have three twenty nine fifteen seven and that's the level order of going three twenty nine fifteen seven That's it. Um, there's not much besides that for here Yeah to do this problem, I think it's the exact same thing actually let me see Yeah, we just do the literally the exact same thing But I think the difference is that we got a flop flip it We got to flip it Every time so let's actually check Let's go out one of the solutions is very very similar The only difference is that I don't want to go that would go to a c++ solution because it's easier to read Okay, yeah Yeah, yeah, okay. Yeah, so this is okay. So Yeah, okay The only difference is that is that every alternate Alternate array what we're going to do is we're going to actually reverse it. So every alternating array. We're going to reverse it And then we're going to print it back. Um So, yeah, let's I'll I'll go over this this value. So This is not that difficult again So we're going to start this again, and we're going to go over the code line by line It's not the see the problem with these coding interviews value like once you know the pattern It's so much easier like you don't have to do all this nonsensical stuff Like once you know the pattern it is so much easier and you don't have to do all of that all right first of all um For him I don't know why you're doing this way, but okay um All right, so what we're gonna do here for him Uh, we're gonna have a queue exactly same queue here And what we're going to do is we're going to push the root onto the queue So the first value of the queue is three. So we're going to push three onto the queue. So we're going to have this I'm going to push three onto the queue so push three onto the queue and While this queue is size is greater than zero Which is the same thing as not empty. Okay. Um, we're going to pop the value of q. All right So oh if it's an all then don't do anything. I don't know why you did it this way But anyway Pop the value of the queue and what we're going to do is we're going to push left side on the right side So we're going to push left Uh nine onto the queue so push nine onto you and the right side 20 you push onto the queue So we have that nine 20. All right. So we're going to push nine and then push 20 um Yeah, so now values is going to be Uh Values is we're going to push now. Yeah. All right. So now we're going to push the actual values So we're going to have a Remember we have a result array and then we're going to have an individual array for each of the level All right, so now we're going to push three onto our values, right? Which is going to be the individual level and that's that All right. So here what they have is that they have a level A level number here and the only difference is that this level number is going to tell us like whether we should reverse the value or not So currently I think level number is going to be zero if I recall. Let me see. Um Where's level? Where are you? Oh, yeah level zero. So right now we're at level zero zero Is it odd? Is it yeah, is it odd if it's odd? We're going to reverse it if it's even we're not okay So it's even we're not going to reverse it. So that's that and then it's not equal zero So we're going to push this onto our array. So yeah I don't know why they recursive we call this Yeah, okay, so we're going to push this onto our array. So now we're going to push Three into our array. So that's that Yeah, and then now we're going to do the same thing pop nine out Add into our array nine Get the left child right child. Nothing's there. No, no. So there's nothing there. Um, there's that and then we're going to pop 20 out We pop 20 out because it's not empty And then we're going to push the children left child's 15 and 7 so we're going to push 15 and 7 into there Push those on Put 20 in here. Okay. So now that's going to be our array 9 20 and that's that then um, what we're going to do is We're going to check is the level Now, uh, is it now odd? So before our level counter was zero, right? But what we're doing to do here is we're going to increment our level counter every time. So plus one every time To our next level. So now the level counter is one and one is odd So now one is odd. We're going to actually just call this reverse method and reverse all the elements So now this these values of 29 is going to get reversed. So we're going to now have nine So this is going to be instead of 9 20. It's going to be 29 So that's that Um that that and then we're going to push increase our level counters We do and then now we're just going to push 29 onto our Right here All right, so we push 29 onto our right here and then that's that It's not empty yet. So There's that we're going to go back to the top. All right 15 push 15 Push 15 into our new value here and get its children 15's child is no no. So we do nothing Um, now we're going to get seven is seven's child is Children no no nothing. So we got remember that I'm going to add seven into array of that Now, um, is our level counter odd? So it's too odd. It's not so we're going to just leave it here And then we're going to push 15 7 into our array And then there's that so then once that's there, that's that and then if we go back to our Output We get the exact right values. We have three 29 29 and then we have 15 seven and these are in its values and it's exact So we have went here and went this way and went that way So, yeah, that's the next value of this problem. I hope you guys enjoy this and we're going to go over another four problems Yeah, this is going to be awesome All right, guys, so we're going to do another problem of binary tree traversal 2 Which is a 107 and this is going to be exactly very similar. So you're using our tree traversal algorithm using breath research, but the only difference is that the um Difference that we're going to start from the bottom and go to the top. So here they want instead of going level order 3 9 20 15 7 right there want to do 15 7 9 20 and then 3 So in my opinion what you could do is you could just Do the regular traversal breath first search and then you could just reverse the array Because then when you reverse the array you just get 15s Use in the end you'll have 15 7 9 20 and then 3 so that's that that's my idea of doing it But uh, we could look at other people's idea um, let's see We'll see other people's idea Okay, so here other people's idea is a little different. Um, they don't want to use a reverse the end array which I understand. Oh, actually they did They did reverse. Yeah Okay Yeah, okay Um, yeah, they did reverse. Um, okay For them. I don't know why they use a clone method That's so weird Okay. All right. Well, we'll look at their java solution and we're going to try to explain how to do their their idea but Essentially, it's very similar and all you have to do is just reverse it. Okay, so we're going to look at the java solution Um, we could actually let me see if they have a c++ solution because I don't like using clone Um, I don't know why you want to clone this. Um It must be a c++ solution Okay, yeah, so this c++ solution is way easier. Okay So level bottom is exactly the same thing. Um, let me see. Do we have a count count q dot size You just do q is not empty Get the front value Okay. Yeah, this this one's much easier. Um, it's it's literally the exact same thing So let's get uh, let's get let's get the picture here Copy here. This is our picture and we're going to do the exact same thing here Um, here's a beginning solution Okay, so very similar we're going to do the exact same algorithm that we did before The only difference is that uh, in the end, we're going to reverse it. Okay, so Is the root empty? Uh, three is not empty. So there's nothing there. So I was just there Okay, um, we're going to have to maintain our 2d array of this And what we're going to do is we're going to have our q. So this is our q our line We're going to push the root of it, which is three so we'll push three onto it And then is our q empty? It's not empty. So what we're going to do is I don't know why they're doing it this way, but hey, we could do this way They're going to loop from zero to the size of our q, which is Um, three so zero the size of q three. Okay, we're going to get the first value of our q Which is three and we're going to pop it out. So we're going to pop it out and then Pop it out and then we're going to get the three's left child, which is going to be, um, Push three's left child, which is nine Onto our q. So we're going to push that onto our q which is over here nine And then we're going to get the right child and push that onto the q of 20 So the right child of three is 20. So we're going to push that onto q. All right, so we have nine and 20, okay Now we're going to do is we're going to push back Current's value. All right, so current's value is three. So we're going to push back three onto Um, a vector a vector v. Okay, so we're going to create a vector another ray and we're going to push three onto it Okay, so that's that and then in the end, um, we're going to push The new array of three into our Results our our residues here our end result here. We're going to push three onto it So now three is going to go here and be over there. Okay That's that And remember we're doing exactly the same thing. Okay. Now we're going to go back to the top nine I'll get the first line nine We've got nine and what we're going to do is we're going to create, uh, Pushed nine's children left and right nine's child left and right and also we do nothing there And then what we're going to do is we're going to push, uh, create a new vector and push nine onto it So here nine vector here. All right. So now we have nine Onto there Okay, so now we have nine onto there and, um Yeah, nine onto there and then, um Yeah, push nine onto there and, uh Yeah, okay push nine back. Okay. Now, uh, since there's two two values in our count, right two values in the size of this So in what they did, I don't know why they did this way between but it's we could do it this way. Okay So Since there's two values, we're going to go back to the top and we're going to get the next value in our next Layer here, which is a 20. So we're going to remove 20 here And then we're going to push 20's left and right child. So left and right child is 15 15 and we're going to right child is seven. So it was seven Okay, so now now we're down here and now what we're going to do is we're going to push the, um Push v onto, uh, 20 onto v so 20 remember 20 here Is onto v so we're going to actually push 20 and add it to our array So 20 is going to be here. So we're going to add 20 here. Okay Now that we're done with uh, two of our values here Since there's two values in our q-right This for loop of the count is going to be over and then we're going to push the results Our array into our result. So we're going to push nine into our resulting array Which is going to be 9 20 and push it here. All right. So now now that's that. Okay, and now we're down here We have 15. Okay, so we have 15 Um, what are we going to do? So 15 we're going back to the top um Is 15 null Okay, 15 we'll go back to the top. Okay. So now we have 15. We're going back to the top um, we're going to Pop 15 out. So we're going to pop 15 out pop 15 So 15 needs to get popped out and we're going to pass in the children of 15 Which is going to be null null and there's null so nothing there. So we're going to put 15 here there Okay, so that's that and then we're going on the right side. We're going to pass in Seven here, which is going to be Seven, I don't know right child seven Pop seven out pop it out because it's not empty yet pop seven out And then we pop seven out and uh, we're going to push these children There's no no children for seven, right? We've got nothing here and nothing there So that's going to be there and then um since we didn't finish our count yet, right our count is Two at more elements left in the array So after that, uh, we're going to push uh seven onto our value of array of 15 seven and that's going to go there and then after that, um We're done with that and we're going to push um our new array of 15 seven into result So we're going to push that there and then we're going to close off a array like that because our queue is empty All right, so now we basically did the same thing of our level traverse as earlier, right But the key here is that um, they wanted bottom up instead of Of top to bottom, right? So here right now it's bottom. Uh, it's top to bottom, right? We have three then we have 9 20 9 20, right 15 7 so that's that But they want us to go 15 7 9 20 and then three so the fun fact about that is that in c plus plus Is a reverse method so we could just call the reverse on here and that's just going to flip everything Over it's going to reverse the whole thing. So if we reverse the whole thing now, we're going to have 15 7 first So 15 7 first We're going to have 9 20 second And then we're going to have three last So that's that and we're going to close it off like that So we have 15 7 9 20 and then three and that's going to be it Okay, that's it and that's the basically the gist of it. Um, the result We could go back to the result description 15 7 9 20 and three so that's that So that's all we have to do and then we just return it and that's it. So that's how you do this problem I hope you guys understand this solution And yeah, we're going to go to the next problem All right guys, we're almost done with this in order successor in bst Basically, we're just going to Given a a binary search tree and a node. We're going to return the smallest key. That's just larger than it So let's say we're at this number one The smallest key that is just larger than one is two. So that's we return two here and Let's say we're at Six here the smallest key that's large just larger than six is nothing here because there's there's nothing here Right, there's nothing so we just returned all all right. Um, yeah So it's basically the smallest value. That's just larger than six If there was a seven we could return seven, but there's no seven here All right, let's just go over the solution. I think you just have to use breadth first search if I recall Yeah Uh Oh, man Oh, yeah, that's it. Huh So, yeah, okay. Um, yeah, all you have to do is just keep going down and um, we need to maintain a node called successor Um, if our current value is greater than or equal to our current root, uh, If our current If the not if the root that we're searching for like the node we're searching for is greater than equal to the current value We set the root equal to the right treat. Otherwise, um, we Have a variable successor that is going to equal to the root And then we're going to set roots to the left side and then we're going to return it So this isn't this isn't really that hard. So let's say I had like I don't know. Let's go here So let's say we had this right. Oh, wait, that's returns and all that's not there. Okay, so let's say we had this, right So what we're going to do is we're just just going to maintain a variable while we're calling to keep going down Uh, I don't know why this the code is like really easy I don't know why they're explaining all this junk here Yeah, um, so we're going to have a variable called successor So we're just going to call this like successor that which is the node that's just smaller just larger than it But we need to return it. Okay Um, while the root is not equal to null so We need to find so we have two values the root which is the What we're currently at two and then we need the know that we're searching for it. So the value that We want to be in order to successor of that node Which is one we want the value the key that is just larger than one the smallest key that's just larger than one Okay, so in this the case would be two so All right, so we have p is equal to one so we need to find the largest node So if I were to go back here and Say p is equal to one, right? We need to find the largest node The smallest node Just larger than one. Okay. The smallest node just larger than one. So in this case, there would just be two Right, it'll just be two. So the smallest node just larger than one. All right, so Basically, all you do is just you just need to keep going down while you maintain the successor And I thought this this solution would be way more difficult, but it's actually not that hard But yeah, all right Got the root we start at two All right, we start at two now um Is one greater than equal to two? Is one greater than equal to two? No, it's not so our successor is going to equal to two And Our root is going to equal to the left side. So we're going to go to the left side of one So remember we start at two now. We're going to go left side of one. Okay Go back to the top Is root equal to null root one is not equal to null. So we're going to be here Is one greater than or equal to one? So in our case is one greater than equal to one. So right now we're here here. Okay Is one greater equals one it is so root is going to equal to right so Right sides null. So Um, yeah, we're gonna write null I'll go back to the top Root is not equal to null Right. So now we're null. So we just return successor So the successor is two So basically what we're doing is that we're just going to keep going down through the tree And we're just going to maintain the variable successor And any time of the value that we're searching for is a greater than equal to The current value we're going to go on the right side And then otherwise we're going to get the successor Which is the we want the smallest by just larger than uh The smallest by just larger than the current value, right? So We're going to have successors going equal to the root which are current value And then we're going to go on the left side. So yeah, so basically we're just going to go like we're going to search right And then if it's larger it we're just keep going right Otherwise, we're going to go left and then we're going to have a variable successor to equal to our current node When whenever we go left so then that way we're going to have the smallest null that's just larger than it Okay, so yeah, it's just like going right and then making sure you keep going right and then If it's not larger we're going to go left and make sure it's that So there's that um, I hope you guys understand this this solution. It's not that hard um, this is more like This isn't even like breath research. Actually, I don't know. But yeah, well, let's go over another problem All right, the next problem is minimum depth of binary tree, which is just Yeah, it's just find the minimum depth Which is the number of nodes along the shortest path from the root node down to the nearest leaf node so Yeah, it's root node here down to the nearest leaf node. So it would be like three to 15 right? So it's one two two, I think Um, yeah We're going to do this with depth first search actually because this is much easier with depth first search So with depth first search, you're just going to keep going down until you reach a node and then you do that It's going to be similar to how you get the height So let's do that real quick. Oh, I already got this thing here. All right. Um, let's see if I find a c++ solution because Yeah, okay. Oh, no, this is yeah, this is a job. I want to look at c++ all right, so um This is more more confusing Actually, it's not that bad All right, so what we're going to do is we're going to call depth first search on our tree node um So what we're going to do is we're going to go return left recursive call on the left side and the recursive call on the right side So that's recursive. So we have three here our root and recursive call left nine and recursive call right 20 so, uh Calling on the left We come back to the top Uh recursive call left nine recursive call right nine So at nine nine's left. There's nothing Uh 20 left. There's nothing so both these would be zero. So it comes back down here. So left is going to be zero. So I think left side Yeah, left is going to be zero. So we're going to have zero here And then um right we're going to recursively call on the right side And um because the left is already zero here. So it's we're actually going to not we don't actually have to Evaluate the right side. So it's going to do that and then Oh, yeah, actually never mind. We still have to evaluate the right side. All right. So we're going on the right side here 20 and then We're going to recursive call left and the right. So left side 15. So we go back to the top 15 And then recursive call left 15 is left is null. So it's nothing. So we're going to turn zero and then um everything's right is No loss return zero So 15s left right are both zeros. So We're going to return a zero plus zero plus one. So just become one So that's this part and then um, yeah, so that's that's the left side and then we're going to go on the right side now So we're going to go on seven Seven goes back to the top left and right are both zero here. So we're going to return zero zero Uh, yeah, and then zero zero plus one. So it becomes plus one. So here we have plus one plus one So we have plus one plus one. So now I have that. Um, so now we have both one one here They're not going to do anything here. Um one one because that's nothing here. Yeah, okay is left side Greater than equal to the right side Yep, so then we're going to actually return the right side. So Since we're doing the minimum one. Yeah, if the left side is greater than equal to the right side We're going to actually return the right side and plus one. So the right side is seven plus one. So Uh, seven's right side is plus one. So a plus one plus one. So it's going to be plus two All right, so that returns this side And then it goes back to the top here and then left is null left to zero right Actually left should be one though, right I feel like left should be one Something's not right. I think I I think I messed this up Um, let's see left side go here left side left. No, no zero zero. Yeah left side should be one So this should be plus one here. My bad. I messed that up. Okay. So now I have one two So now this side is this side's left is one This side's right is two. So we have that and that's that is left greater than equal to right. Uh, no, it's not So we're going to return, um Some left plus one. So we're going to return left side of plus one plus one. So it's going to be plus two So in the end we're going to have plus two So yeah, so essentially what they're doing is they're going to keep recursively calling going down and every time we're going to Increase one for the number of branches If they're both zero, we're going to have plus one if left side is greater than equal right side What we're going to do is we're going to actually return The smaller one which is right side plus one and uh, yeah, otherwise we're going to return the The other side left side plus one So yeah, this is just getting the what should have our side is smaller and it's going to add that No return that and the reason why we plus one every time is because we're going down the tree every single time So we're going to add one. Um, I feel like you could just use minimum here I don't know why they did it this way. Yeah, but this is this is a way to solve the problem We're just keep going down all the time Yeah, hope you guys enjoy this this part and we're going to go over the next problem all right, um Populating next right pointers in each node Um, this question is going to be exactly the same thing We're going to give it a binary search tree But this time we're going to add a new pointer and this new pointer is going to point to whatever's on the right side so we have one We want to create a new binary search tree where Whichever neuro node is we're going to point it to null and it's going to point to whatever's on the right side So ones is going to point on the right side. That's that two's right side is three So we're going to two is going to point to three and three is going to point to null Four is going to point to five five is going to point to six six is going to point to seven and seven is going to point to null So so we're giving a regular binary search tree, but we want to create something like this right where whichever value We're going to point it to the whatever's on the right side So yeah, we just have to make sure they all point to the values on the right side So I think what we could do is do a same thing as an in order traversal and just have to make sure we set all the nodes properly um Yeah level order traversal. So yeah, so we're going to do a level order traversal But this time we're going to mark it and set its value in it properly Um, let's just go over the code right real quick Okay, so it's it's not that hard. Uh, we can even go over the Java code All right, um varies very exactly similar to what we have before Just make sure that we actually do this properly all right, so We want to create something like this on on the on the right side, but let's actually just get rid of this for now So we want to create something like this, but let's have some space All right, so let's go to the editorial. All right, so um We add one to the value of our q so our q is this and we're going to add one to it All right, and then what we're going to do is we're going to get the size of the q and we're going to loop Through all the levels. Um, the reason why we're doing this this for loop is just because If the number of children isn't too right but Our size always going to be two because it's a binary search tree if we're always like an n area tree then yeah, this would make sense But yeah, this is why we have it. All right pop the value on the front of the q. So we pop one Um If it's less than the size, oh, yeah, all right pop the value on the next pop one Um, if we're not at the last value, right if we're not at the last value We don't want to have like bad connections So if i is if our current like node number is smaller than the last value then we need to set The next point or pointing to whichever is there. Okay, so one's next is going to point to Whichever is on the front of our q so our value on the front our q is null So one's point is going to point to null one pointer next is going to point to null. All right, so that's that Um, then we need to add the two children. So add two and three So you have that it goes back to the top pop two out of the stack and i'll stack out of the q and um two Oh, yeah Out of the q is two's let is two's a pointer Is It's our current i not as small in the last value of our size, right? We make sure that we have to at least have a two nodes in order to connect them So if it is so i So right now i is one right i is smaller than size minus one, which is like Yeah, so two is i one is smaller than Two minus one zero is still smaller than two is one. Okay, so we do that um Two's next is going to point to The top of our q which is three so two's next two is going to point to three Okay, so then two points of three Um, we're going to add the children of two. So we're going to add four and five And that's that Um get the next value we'll go to the top pop three out of the stack That out of the q my bad out of the q push on the two little children six and seven Right three's children is six and seven. So we push those onto the q. Um is our value Of uh zero one two zero. Is it smaller than the size of it? Yes, it is So we're going to set uh four's value So, yeah Um, yeah, is this smaller than it? Yeah, so out of four's value remove it, right? And we're going to add um Four's value is going to point to the first value on the right side, which is five So we're going to point it to five I think three we have to point to null, but I think originally they said everything points to null originally so I think I think all these are originally set to null anyway So that's a good part Um, yeah, so we four four is always going to point to five Um remove five from the stack now. So we pop five from the stack add its children Five children is null. So we don't need to add any of that um Yeah, five children is not we don't have any of that. So then we Move those um pop six Oh, yeah five size uh our current iteration Is um not to the end yet. It's not to the end yet. So six is not to the not Our current value is still smaller than size minus one. So we're going to Point uh five point to the next value of the top of our queue, which is six So five is going to point to the next value of six Go back to the top remove six from the queue Push six's uh children's null null. So that's going to be nothing there. Um after that Nothing's going to be there, right? Um remove seven from the queue. So we remove seven from the queue Is six smaller than the size minus one? Yes, it is. We're going to point six six's node point to seven next point to seven um We'll go back to the top seven and Yeah, add seven's children seven children's null. So we do nothing there go back to the top Um, yeah, our queue is empty now and then that's it. So here we're gonna This is gonna be null because like all the values are originally set to null This check is important because we don't want to establish any wrong connection The queue would contain two nodes at a level at most any time this ensures that we don't establish the next point is beyond the end of the level So yeah, this is this check is just make sure making sure that we go Don't don't establish any value to the next level. So we don't go beyond the size of this level So yeah, that's the gist of this code. Um, then we return the root, which is going to be one's root And that's it. Yeah, so we don't have to do anything for this. Um, there's not much to do here Hope you guys enjoy this problem and then we're gonna do one last problem and then that's it Then i'm going to render it and then I hope you guys enjoy this video. All right, so we're gonna go on the next value of the tree so Binary tree right side view we're gonna do binary tree right side view Okay, so given the root of a binary tree imagine we're standing on the right side of it And then we're just gonna see from top to bottom. So if we're if we're just standing right here Let's say i'm just right here. I'm gonna see all the values from top to bottom. So one three and four That's what we're gonna do. All right, so how do we do this? It's not that hard um It's really not that hard, uh Yeah, I'm you it's it's these problems are not that difficult once you understand the the pattern of it but It's probably a You could do is breath for a search or death for a search. Let's just keep all the glory down cube by Level size measurements. Actually. I think dfs is the easiest one here. Um, do we want to use breath for a search? All right. Well, we use breath for a search because this is a breath for a search Problem. All right. All right. Um very similar same thing Start at the root one. We're gonna have our q right here one Add one to the q. All right. So we add a one to the q. All right is our q empty It is we're gonna get the level length Which is uh the size of the q. Uh, it's just one So we're gonna do that loop through zero to the one size of q get the first value of the q. So remove one Um, if it's equal to level minus one. So if we're on the rightmost element, so level is one Uh, level length is One right now. So one minus one is zero. So i is equal to zero So we're gonna add this to our right side. So we're gonna have an array of right side Which is going to be this b1 add that there um Yeah, add that there and then we're add the children Add the child nodes as a q. So we add two and three. So we add two and then we're gonna add three So we add two and three there and then now We're going to check is the q empty. So this level to length is two now because we have Two nodes now in the q. So we're gonna loop from zero to two And um, yeah, we're gonna get the top value over two of our q So we remove two from it and then we're gonna add um, yeah, we need to check is to the rightmost element. So i is going to equal to zero is zero less than two it is um is uh Is zero equal to two minus one. So two minus one is one Right is zero equal to one. Nope. So two is not the rightmost element. So we do nothing there. We don't add it We still need to add the children of two. So we're gonna add the left and right child Two's left child is null. So we don't add it two's right child is five. So we're gonna add five to our q Bam, we're at there. Then um, we're going to go back to the top of this and Yeah, we're gonna go back to the top of this and then um, we're gonna check Remove three the topmost value of our q. So it would be three three gets removed three gets removed. We're gonna check if Uh, three is equal to the uh Size of our q. So if it's the rightmost element the size of our q is two values right now So is zero equal to uh, two minus one one Actually, you know Our size of our q is Two is size of our q is two. Wait. Yeah Yeah, yeah. Okay. Our size of our q is three Um, do we add? Oh, we didn't add five. Okay. We're supposed to add five afterwards Somebody's not right I don't know. Okay. So we're at three, right? Three is i equal to rightmost element. Um, it's this is supposed to be the rightmost element. I is supposed to be Oh, it's Pre increment plus plus one. So yeah, um is one equal to uh length the length two two minus one one Yeah, it is so we're gonna add three to our rightmost element. So it's gonna be three is gonna get added to here Into right side right side is oh right side just a list. Okay. So we're actually returning a single list So we're turning a single list so we're gonna add Three is gonna get added right here. All right. So we did that. Um Three's child is four. So we're gonna add four to the q And yeah, we go back to the top five remove five from the q five's left as uh is one equal to Two minus one zero equal to two minus one. Nope. So we don't add five to the To our cubes, right? So we're gonna remove that Um Yeah, and then we're gonna add left and right. There's no null. So we'll do that Um four goes back. We go back to the top here. Um our size of our level now is I think our server level is still two, right? Um, yeah Sizer level. Oh, I know it's one it's one zero zero, uh, we go zero here and then, um I is equal to level minus one. So are we at the Rightmost element of level minus one. Um, so we are now we're at the rightmost element four So we're gonna add four to the right side. So it's gonna add four here And then we're going to remove it Um, so this pulse will remove it. So yeah four is gone Add the two children. There's nothing on there. And then after that, we just have the right side of one three four So then we just return this one three and four So yeah, that's the gist of it. Um, I hope you guys enjoyed this video That was very quick breath first search the right side. Um to check the rightmost element That is what you do. Uh, I had like I think I messed up a little bit on doing the counting But yeah, it's not it's not that bad. It's the right the last Note is on the level size minus one and you could push it on to it Yeah, I hope you guys enjoyed this video rate come subscribe. I'll check you guys later. Peace