 What's up guys, my name is Michael and welcome to my YouTube channel today We're gonna do another elite code challenge. This is called an airy tree post-order traversal Excuse me So Remember how the last video we did pre-order traversal. We're now we're gonna do post-order traversal And what's the difference? Well in this example you see one three two four five six Instead of going to the node first and then it's children We're gonna go to the children first and then the node. So what's one's children three two four What's three's children five six? So we go five six then three and we go to the rest of one's children, which is two Four and one so it's five six three two four one okay so How are you gonna do this? We are going to I'm gonna show you the iterative on how iterative of recursive way first and then the iterative way iterative right dad just say way right whatever, okay, so We're gonna basically do the same thing as the same thing as what we did before if it's null We're just gonna return an empty to return. So I'm gonna create a vector int to return This is just me the ray that we're returning for the pre-order post-order traversal With the values of the post-order traversal. We return to return if it's null Otherwise, we're going to call post-order And it's gonna help post-order helper and then we're gonna pass in the root and then we're going to pass in a vector To return which is a vector that we're passing in that we're gonna add values to this To return for each post-order that we're going through and then we are going to return to return, okay? So now let's think about how am I gonna? do the post-order helper while we need to Well post-order helper is gonna be a void function and it's going to be called post-order helper And it's going to take in a node for the root of whatever Yeah, whatever root we're currently let's say current current out just and to make it like not so to ridiculously complicated We'll pass in the to return The way that we are returning basically the ray that The values that we're gonna add to it that we're returning for each of the post-order traversal And what are we gonna do? Well? instead of doing Excuse me We are going to add All the values from its children first To to return all the values of its children so occur children at I Children at I which is a node and then we got to do push back to add node Val, okay So this this basically just adds every single child of the current node and we're adding its value to to return Then after that we're gonna do to return dot push back add the currents nodes value So instead of actually doing the instead of actually doing we're adding the The node first the current over at currently we're actually going to add its children first So five six and then we're gonna add the current node So this is gonna do five six then three And then because three two four are the children of one. We're gonna do five six three Two four and then one and that's basically it That should be all it is it's from the code Please work. Please work. Please work. Please work. Please work Three two file four one three two four one. Okay, something's not right three two four one Did I add I did I added the children first Go through its children. Oh I didn't okay. Whoops Don't add this first it's supposed to be a We're doing a recursively so we're I was supposed to do this where I call and post-order helper again, and we're gonna pass in the Each children and to return. Yeah, because we have to go through the children first Then call it the children again. Go ahead. It's children that add then add the values. Okay So yeah, it's also add call post-order helper again And we're gonna call post-order to help her on its children for the current nodes children and Each time we do it we add it to to return and then we got the expected values five six three four five six three two four one and then Submit it and it got accepted. Okay, so that's that's how you do it recursively That's how I would do it and I'll We're gonna talk about how to do it iteratively. Okay, this is gonna be more complicated All right guys, so I'm gonna show you how to do it how to do it iteratively. So how would you do this iteratively? Well, you would actually need to use two stacks. I Pretty sure you need to use two stacks. There might be a way to use one stack, but this is how I would do it We first may create maintain two stacks. So this would be one stack into the other stack We are going to push the first node onto the stack So the root one and then what we're gonna do is we're going we are going to pop this out of the stack and Then push it onto the output stack. So this output is gonna be one. So this is out It's output and that's input Then we are going to push all the left all the nodes from left to right into our input stack from one So all the children of one. So that's three two and four then after that we're going to pop Four out of the stack and push it into our output stack then we're gonna add the nodes of The children of four so from left to right and that's nothing. Okay, so that'll be nothing Then we're gonna pop two and we're gonna pop two out of the Add it to our output stack. So this would be two and add two's children into the input stack, which is nothing then we're gonna pop three add three to the output stack then add the the nodes of The children nodes of three which is five and six to push those onto the input stack Okay, we're gonna pop six Add it to our output stack then We're gonna add six children from left to right which is nothing then we're gonna pop five out of the Input stack add its children to the input stack, which is nothing Add it to the output and then we're done Now all we got to do is just keep popping of our output stack from pop five six three two four one And add those to our array. So we're gonna pop five Six Three two four pop four one and That's gonna be our Final array that matches five six three two four one five six three two four one and that's how you would do it So I'm gonna implement it right now Okay, so we have I have a current stack right here Yeah, I have a current stack So what I'm gonna do is let me just do the delete this and so I first push the root onto the stack I need an So I need an output stack also. So I need to stack node Output stack output stack output stack. So that's our output stack. We push the root onto the stack while our current stack is Not empty While current stack is not empty We are going to get the top value of our current stack, which is a The top value of our current stack. I'm gonna call it top Top whatever top valve top note top note Okay, so I'm gonna get top note and I'm gonna pop for the Pop the first value off of the that stack I'm going to push it onto our output stack Push the top node to our output stack Okay, so now that's gonna push that then I'm going to go to the children of top node equals top node dot Begin How it not equal to top node n It plus plus So I'm gonna do that So I'm gonna go through all the children of top node top node dot whoops dot Top node children dot begin top node dot top node Children that and That and plus plus. Okay, so this is gonna go through all the children and I'm gonna push it onto my current stack of Each children after that's done. I'm going to have a stack of The values of here and there and I'm going to do while output stack Is not empty so output stack is not empty. I will get each node top node top node from each output stack dot top I'm gonna pop it so I'm gonna pop pop the value I'm gonna get the top node of our output stack and then we are going to add it to Our array, okay, so we're gonna add it to to return so to return Dot push. So this is gonna add it to the backup to return. So top node Gonna add its value and then after that we're going to return To return So this is how you would normally do it Reason why we need two stacks is otherwise we would have to reverse the values, which is something I don't want to do And I would take more time I guess but this makes more sense So yeah, now we have five six three two four one submit the code And it got accepted So yeah, that's how you would do this using a post-order traversal. You would need two stacks and You would add the children You would need to push the children onto it and then pop it all afterwards when you're done So that's how you would do it Raycom subscribe. I'll check you guys later. Peace