 What's up guys, my name is Michael and welcome to my YouTube channel today We're gonna do another leak code challenge called an area tree pre-order traversal. So given an NRA tree Return the pre-order traversal of its nodes values. So for example in one Three two four five six. What does the pre-order traversal do? Well, it first takes prints out the current node. So in this case, it's one then it prints out the children and Before it prints out the children it does the children of that node. Okay. Oh, no, no, no my bad Excuse me It prints out the node and it does its children. Okay, so that's how pre-order traversal works. Okay, so my fault So it does one Three and it does the children of three Right, so this is five and then six Then because it comes back up here and it does the rest of the children of one So in this case would be two and four So that's why the pre-order traversal is one three five six two and four So that's why the return of the order is one three five six two and four So our job is just to return a vector that has the pre-order traversal and that's pretty much it So how do we do this? Well? I'm gonna give you time to do it and then there's two ways to do it You could use iteration method or you could use recursive method I'll go over the recursive method first because it's easier to understand and I'll do the iteration method later. Okay All right, so how do we do this recursively? Well, first of all First of all, we got to do something We got to check if the root is null Because if the root is null Then we're just returning an empty Empty vector an empty array, right? So here I'll do a vector in to return If the root is null, we'll just return to return Okay, so return to return. Okay, so now otherwise we have to do a pre-order traversal And what we're gonna do is we are going to pass in Excuse me Excuse me. I'm still a little sick. We are going to do call and pre-order helper Which is a pre-order traversal what we're gonna do is we're going to pass in the node Which is the current root that we're at and then we're going to pass in the to return Vector in order to add to it. So we're gonna add add the values of The pre-order traversal into our vector and then we are going to after that we're gonna return to return so now we're now all our Our main job hat we have to do now is just to basically implement this method called pre-order helper, which is just going to essentially add the values of the root into the pre-order traversal of the values of the root into our vector to return Okay, so now I'm gonna call it create a create a helper method called void pre-order helper and it's going to take in a node root and It's going to pass in by reference vector int I don't know let's say to return whatever It's going to pass in by reference Okay, so What exactly do we have to do now? well first of all What we have to do is we have to add our Current root that we are at into to return. Okay, so we're gonna do to return In our helper method because we have to add the current Current know that we're at first remember is one three five six two four So we got to do to return not push back and we're gonna push back roots value Okay, so now we read now we added to returns Roots value. Okay, so now Now our job is to go through the children of each of Root, okay, we're gonna go through everything every single children of Root and then we are going to add the values into to return So to do that, we are going to go to this for Let's see. What is the okay? So it's a vector. So we're going to do int I equals to zero I is less than Roots children Dot size I Plus plus That's going to go through all the value the children of root Which is three to four in this case will be three to four and what are we gonna do? We are going to call pre-order traversal and pass in essentially pass in we're gonna recursively call pre-order traversal and pass in our The current every single child of root. Okay, that's we're gonna do Okay, so now that will Recursively add those values in so we're gonna call pre-order Helper root What my bad? Let's see. It would be roots children at Values of I which is yeah, so ever we're gonna go through every single children of root pass that in We'll recursively call in our pre-order helper and then after that we are going to pass it into return so essentially this is going to go through every single children and Recursively pass in the values for every single children and it's going to do this thing again Once more it's gonna add the nodes and add that its children and keeps going and going after that We're gonna return to return Actually, we don't have to return to return because it's we're passing my reference so to return would already get Changed. Okay, so that's all we have to do in this case After that we're gonna go down here and yeah, that's that's pretty much how you do it Because in this case once we pass in pre-order helper I'm passing every chill of every roots children. It's gonna come back up here And it's gonna add whatever value that is go through its children again keep going until everything's done So it's gonna do one go to the children of it, which is three What is three children five six a lad three five six after that it goes to the will come back here up here And I will do the next child child of one which is two and since two has no children It'll do nothing then it goes to the next child of one which is four and then after that it would return The pre-order traversal so let's run this code and see what it does hold up I'm sure take this long. Okay, so it does it One three five six two four one three five six two four. Let's submit this and it got accepted So that's how you do this recursively Basically, all you do as you go you create a helper method and you pass in the root you add the root to your Vector you're returning to and then you go through its children and recursively call it based on the children of that That's how you do it recursively now. I'm gonna show you guys how to use it iteratively All right guys, so how do you do this iteratively? Well, we got to think about how you would do this So we're gonna use a stack First what we're gonna do we're gonna add the first root of our value Which is one into our stack and while our stack is not empty. We're going to pop the top part of our stack to pop top the top part of our value in our stack off of the array Off of the stack. Okay, so we're gonna pop this value one off the stack and we're gonna add it to our array So here I'm gonna have an array one Then we're gonna look to its children, which is one's children is four two We're going backwards through one's children, which is four two three and We're gonna add it to the our stack reason why we're going backwards is because if we go forwards We're gonna end up going to four first, which is not what we want to do We want to add the values backwards. So then when we pop we add we pop the values of three Which is the right order. So we're gonna go backwards and add four Then two then three Okay, so now all we add so we added the children of one, which is four two three And this is we're adding it backwards four two three now what we're gonna do We're gonna pop the first value of our stack pop three out So now three is now out of our stack and we added to our array Now we check now we're gonna recursively add the values of three's children Backwards is six and five. So we're gonna add six and then five Now what we're gonna do is we are going to pop our values off of the first top part of our stack Pop five out and add it to our array. So here I'm gonna pop five and add to our array So here I'm gonna add five to our array now all after we add five to our array. We are going to essentially Go to its children will five has no children So we're done with that then we are going to pop the next value off of our stack Which is six take six out of our stack add it to our array Then does six have any children six doesn't have any children. So we're done with that We pop the next value off of our stack, which is two What is two? Yeah, if we add two to our array Does two have any children? It doesn't have any children. So we're done with that then we need to add we pop four off of our stack and Add it to our array Does four have any children? It doesn't have any children. We're done. Then we return our two array. So that's how you do That's how this would work. Ideally inside our Methods that we're gonna do so now how do you code it? I'll code it right now with you guys We just get rid of them. Just close the paint Okay, so I first have our vector to return and I'm returning to return first I'm gonna do is check if Root is null because if root is null then we just return to return Now we're gonna do is we're gonna create a value called Kerr. We're gonna create a stack Stack it's gonna be node and It's going to have it's good. We're gonna call it Kerr stack We're just our stack that we're keeping track of the values that we're adding Purchasing and popping so What am I gonna do? I'm going to push The first root Into our stack So I push the first root into our stack and then I'm gonna do while our stack is not empty. So Kerr stack Is not empty What am I gonna do? Well, I'm going to take the first top value The top node off of our stack. So we're gonna do top top node Is it equal to our current stack dot top? then after that I'm going to pop the value of Our node off of our stack and then I am going to essentially go through and I'm going to push the top value of our stack Top value the top value of our the top the top node its value into our Vector that we are returning so to return dot Push back so we're gonna add this to our vector that we're turning like how I showed you And we're gonna add its value Okay You guys understand what I'm doing so far now now we have to We're iteratively go through it top nodes children and we're gonna keep pushing it into the stack But we're gonna push it backwards starting from four two and three in order to make sure that we don't get three two In order to make sure that it's going correctly because if we push four if we push three two and four And then we take out the top part of our stack. It'll go it'll go to four We'll go through four's children first. So we're actually gonna add Backwards from our children of one. So we're gonna push four two and three So I'm gonna do a four I'm actually gonna create an iterator That's gonna go through backwards. Is it gonna be iterator is gonna go from Top node children Dot are begin which is a reverse iterator. So it goes backwards then Todd iterator is less than iterator is not equal to top node children dot are and iterator we're gonna Pre increment iterator. Okay That's basically how you would do this. We're how do you iterate it backwards and then we're going to push the values of Each value of the children from backwards from four two and three into our stack And that's how you do it run the code And let's see what it does. It does it correctly one three five six two four. Let's submit the value and Let's see what it does and it got accepted. So that's how you would do it iteratively using a stack Ray com subscribe. I hope you guys enjoyed this video and I hope you learned something from the video. I'll check you guys later. Peace