 What's up guys, my name is Michael and welcome to my youtube channel today. We're gonna do another leak code challenge called binary tree paths and What this question is basically you're given a binary tree and you have to return all the route to leave paths so in this case 1 2 3 5 right the route to leave paths are like 1 2 5 because at this end 5 is no more Children like left and right. There's no children here and the other path is 1 3 1 3 because there's no more children here Okay, so this is actually not a that bad of a question because In terms of depth first search is not that bad So what we basically have to do is we have to keep going down until we reach a node that has no more children And while we do that we're gonna add every single every single Value to our path every node that we you already went through until we reached that children so in this case We'll be one we're gonna add one two five up five has no more children. Yeah, that's gonna be a path and Then we're gonna add a then after that we're gonna start from one again And we're gonna like one three up three has no more children. That's it. We're gonna have one three Okay, so how do we do this first? We need to know First we need to have a vector or array Whatever you call it and then we're gonna call this pass and what this is gonna do is this it's just going to be a path that It's gonna be a vector that stores all the paths that we're going through and I'm gonna return this just to make sure that I'm saying right now. Okay, so what are we gonna do? Okay, we're gonna use depth first search and what is this depth first search gonna do DFS? Okay, well, we need to pass in the root because that's something that we need to do Once we have like the current route that we're on Not we have the current know that we're on we have to pass in the current know that we're on and then We have to do the checking so like that. All right. I'm gonna pass in a path or Previous path. I'm gonna call this previous path This is gonna be the previous paths that We're gonna add on so like in this case one two five the previous path is gonna be one two all the values for one The previous upon two It's gonna be the nodes that were added for the path that we're on like one two and then Then once we reach like the node that has no children five We're gonna add one two five. So the previous path is basically just maybe I shouldn't even call it previous path It's it's just like your current path basically That you're on and you're gonna add your The children not children the nodes to the previous path so to the current path, right? So if you have like one two five, you're gonna add one two to your current path And then one three is gonna be in this case of the binary tree pass of one three It's gonna add one to your current path. And then yeah, I Also need to take in the my vector Paths and the reason why I'm taking this is because if I pass in the path I Need to add my paths My current path to the path whenever I reach and know that has no children So like one two five five has no more children So it's gonna add one two and five two my paths and let's say in one three. I want to add one and three I'm gonna add the path one three to my paths. So that's what we're gonna do Okay, so what am I gonna do I'm gonna call DFS. Oh Yeah, I'm gonna call DFS and I'm gonna pass in my route And then I'm gonna pass in a current path and then I'm gonna pass in my path And what is my current path? Well, I'm gonna start with an empty string because that's how you should start with Because you didn't start it. You didn't go through any paths yet. So it's gonna be empty. Okay So once I call DFS, I need to check if my current is not null current node I'm on not know Yeah, because if it's null then you can't really do anything you can't go anywhere So well, you don't want to add null values to your path the current path you're on. Okay Now what we need to do is we need to check We need to check if our current Know that we're on right if it has a left or right child. So let's see. Yeah, if left. So if left If it's left child is not null and Oh, no, nothing. Okay, if it's left child is null And it's right child is null What does that mean? That means that we have to add That means we're on the final path Right, if they're both if the children are no, so in this case five fives children is null Like there's nothing below five. That means we have to add the current path to our path list of paths Okay, so pass. I'm gonna push back Current path, okay but Remember, um, I need to add in The current node that I'm on also, right? So I'm gonna add in my last node of value of five to my current path and then I'll add My current path to pass because I didn't add that yet. Like I didn't add five to my Pass we're assuming that we're assuming that we already have the current path like one two That one two is like is already added to our current path So when we reach an end of a note of five Where there is no children? Then we could we have to add the current node the last node to our current path Okay, so that's what I'm gonna do here. So I'm gonna a current path Is equal to current path and I'm gonna add in a The the value of my current node, okay So in this case in order to get the value we have to do current Value I'm gonna convert this to a two string. So this is gonna add the value to our last This is gonna add the value five to our last The value the value of the last node to our paths. So in this case one two We're gonna add five. Okay, that's gonna add five Okay All right. Now this is something is different more difficult Now what we got to do is that? Let's say that we the left child and the right child are not null or one of them is not not null That means you have to go to that left child or the right child. Okay, so regardless See if one of these are not null that means I have to go either to the left or the right So I'm gonna copy DFS and do this twice What am I gonna do? I am going to go to the left This one DFS. I'm gonna go to the left because we know one of them is not null, right and Yeah, the other one I'm gonna go to the right Like at least one of them not know so in order to check it I'll just go left and right so this is gonna recursively go to the left left side of the tree So this part so if one of them is not null, we're gonna go to the left and the right, okay? And we're gonna still pass in our paths But now what's different is that now instead of just our current path is gonna be different Okay, so if I'm going to the left or the right right, I Need to add my current node the current the current value of my current node and I have to add an arrow right The reason why I have to add an arrow is because we're not done We're not we didn't reach the end of our path. So in this case like let's say I did one two So okay in this case one one has a left child to and it has a right child three right They both not know right so regardless We have to go either to left side or the right side. So what I do is I go to the left side Okay, and then once I reach the end of left side, I'm gonna go to the right side So that's what these two cases stand for so first I'm gonna go to the left side current to the left side And I'm gonna get the path from the left side Okay, so what am I gonna do? I need to add the current node that I'm currently on To my current path And I need to add an arrow So I'm gonna add one and then an arrow The reason why is because I'm not at the end of my note my path yet. I'm not at the end Like I did we didn't reach the where the children are no we're fives and all where there's no nothing There's no more children for five. So we didn't reach the end yet So we have to add our current node and then we have to add an arrow. So that's what I'm gonna do So this is gonna be a current path Is going equal to current path plus? Let's see We're gonna add our two string Current value and then we're gonna add An arrow then after that I'm gonna pass this in and do current path Okay, so once you pass this in as current path, what's it's gonna happen is gonna add the arrow Yeah, the current know you're on it's gonna add the arrow and then it's gonna recursively do the same thing to the rest Okay, um, I Actually don't want to pass this in by reference because I'm not gonna change current path because we're every time we change current But we're just gonna add it into the Actually, maybe I should pass by reference. Yeah, okay. Um Now we're gonna run the code just to see if it works Okay, so we have some issues here We have one two five one two five three. Okay, so the problem is that When you reach let's say one two five one two five three, why is that in three? Let me see. Let me see. Okay. Let me see what I did before just to make sure that I'm on the right track Yeah, I did that I did that yeah plus Yeah, oh, okay, so our issue is that we were passing in by Reference and that actually modifies the current path every time our current path is gonna change every time So that's why we don't need to pass in by reference Because we're gonna pass in a current path to it every single time. So Okay, so I got that accepted and I'm gonna submit and I got accepted So that that's basically how you would do this question. You would change your path Over and over again Pass it in and then you would call DFS on the left side DFS on the right side when you reach to the end where the both Children are null. That's when you actually add your current path to the list of paths that you have So that's how you do this question. So I hope you guys enjoy this tutorial rate com subscribe. I'll check you guys later