 Alright guys, I haven't done a video in a long time because I've just been procrastinating too much and Yeah, I just I don't know. I was kind of relaxing in summer. Anyway, today. We're gonna go over another leecho problem for Google This is called step-by-step directions from a binary tree node tree node to another so you're given a binary tree right and they represent numbers from one end Okay, and then You're also given the start value and the end value, which is the destination value, okay? So we have a destination start and an end Now we what we need to do is we need to find the shortest path Starting from node s and and then and ending at node t. So what that means is that yeah So we're giving it. We're giving the starting node and the ending node and we need to find the shortest path. Okay, so We also want to generate the step-by-step directions When we're going through the path. So what this means is that L r and u are the letters that we want to generate for our step-by-step directions So l means to go from a node to the left child r means to go to the right child and u means to go to the parent So if we're gonna go look at this Let's actually copy this image and go to paint so in this situation So here our start node if I for some reason this is not connecting up. Whoops My bad Some difficulties here. So if our start node here is three So they said our start is three and our end is six. So there's our start and then our end is six So we're starting at three here and then we want to end at six. So you want to end up here, okay? so As you can see here, like the shortest path would be like going this way. So if we look at three What what do we have to do to get to six well since they said we only could use every step there's L R and you right go left go right or go up. So in for three In order to get to six, right? We have to go up. So we went up once right went up and we went up again From one to five and then here from five we have to go to the right because For five to the right to go from five to two we're going to the right So we're gonna go R and then from two to six. We're going to the left. So that's L so our answer here would be you you are L, okay and And yeah, you you are L. So that's the shortest path here for the output okay, and Now the second example is pretty much very similar If we just go from here to In order to go from two to one We the only way to do this is go to the left right because there's nothing to the right and there's nothing Down nothing up. So to go from two to one is left. So the answer would just be L So yeah, so the two is the starting nodes to start and this is the ending node destination, okay, so how do you do this problem? One way is to actually do lowest common ancestor so what that means is that We want to find the ancestor that is like they both shared like the smallest ancestor that they both share So if we go back to the original problem here the original test case so If I find the lowest common so the lowest common ancestor lowest common ancestor LCA right lowest common ancestor. It just means is that what is like the smallest parent that both of them share? right, so in this case this six and six and three they both share Five as like the lowest common ancestor as a parent, right? because if we were to just like Try to find the parent that they both share five is the one that Is shared by three and six and what I mean parent. I mean like the one that is above it So like if this six wasn't here if this six wasn't actually here and said it was like Oh crap So let's say the six was not here, but the six was like right here Then the lowest common ancestor would be one Okay, because they both share the common ancestor of a parent of one But that's not the case in this case. Okay Yeah, so what's good about it is that if you define the lowest common ancestor if you find five All you have to do is just go up like from three to five My bad. So we find five and then we just have to find the values of both like six and three So we just have to go down To whatever values to three and then whatever value six So so here the algorithm would be like lowest common ancestor fine of Of three and six which would give us five and then now we just have to go from five and go down to Three are starting node and then we have to go five and go down to six ending node Okay, so starting node and ending node Then um Going from five to three since we know that this is like always on the left side For the lowest common ancestor on the left we could just always Append up for that right because it's always gonna go up like it from three to five It's never gonna go down or go right or go left So the only way you could do is just go up So once we find the lowest common ancestor and go from five to three We could just like as we travel from five to three. We could just every time I go left or Right, we just replace the values with up So in this case going from five to three If I go five to three I'll go like left left and then I go three right. I assume I go five to three So this would go a left left three LL and then We get to three so then what I would do is I just replace these with ups because to go from three to five No matter what happens, whatever whatever, whatever I do. I have to go up, right? So this one you just replace these with ups, so that'll be you you right so three to five We'll just always go up So this would be you and you and then yeah, we place these with you you and then if I go from Five to six it would just go right and then go left right left So they'll be right and then left right because if I use it do a tree traversal from five to six The only way is to go like right and left right You could use any type of tree traversal as long as you are able to like get down to your children But yeah, this would be right and left And then once you have that all you have to do is just append both of these together So we have you you and are you L and we just append you you and are L and that would be our answer, okay? So yeah, just find lowest common ancestor between the three The dear two nodes which will give you your parent and from there I just traverse downwards from that to the start node and that to the end node Replace all the values of left with up right because that's just that's where you go up, right? And then yeah, then a pen near to Append your two strings together and just return. So yeah, that's basically it just the algorithm I can show you guys I can make a whole video on lowest common ancestor but here's Like the algorithm how lowest common ancestor would work. So anyway, let's just Let's just start here So here's a code get direction and we have the root the start node destination So what I do is here is I'm just going to replace the root to you equal the lowest common ancestor Which is LCA of root start value and destination value and I have the two strings of F and S F and S just represents like Represents the the directions that we're going as we go downwards, right? So this would be like F and this would be like S, right or two straight Okay So I'll be going from Yeah, going from here then um, we're gonna get path from Our start value and From a root to our start value right and then we're gonna get path from root to destination value and each time We're gonna suspend it every time we go down. We're gonna append it to our string For F and S for our path, right our path Then what we're gonna do is we're going to replace all the values of F with up Right, like I said before early in this video because no matter what You're gonna have to go up on your left node, right for the left node The left side to get to your parent you have to go up like there's no way you could go right or down So yeah here We just replace the the path for the for F of the left node to be always up and then I just return The the two strings combine together so F plus S can coordinate together Okay, so yeah F and S represents the paths for both of the string representing each of the paths from our root of lowest common answer to the starting value and Root from lowest common answers or to Destination value, okay, so that's what F and S mean Okay, so now let's think about how to get path Yeah, so this is this is how to get to the path Let's actually go look at lowest common ancestor first because that's I think that's the hard part of this about this problem Okay, so to get the lowest common ancestor Here's what you do So here if the root is knowledge just return all right the first case So if our root is equal to our start value, right? That means that we're kind of done So like if you look at this Yeah So to get lowest common instead of being three and six Yeah, so whoa Well, I kind of like assuming in too much Okay, so if I want to get three to six right So I'm gonna go from Yeah, okay, so currently in my root is at five right so what I'm gonna do is I'm just gonna Go to the left and go to the right Okay and Yeah, and then I'm gonna recursively call Keep searching on the left node in the right now So if my root node is equal to the start value, I'm just gonna return the root Otherwise, I'm gonna return Otherwise if they're my root value is equal to destination value I'm gonna I'm gonna return the root the reason why we do this is that we want to check like Which node that we're on if that makes sense so So here five is equal five is our root node, right? So we're trying to get lowest common answer three and six So our new root node is always like the starting value of five. So this is our root and What we're gonna do is we're gonna go just Check is equal to our start value So five is not equal to our start value. So we do nothing then we're gonna check it does equal to our destination value So our destination value is six Right, so so remember three is our start Six is our destination Okay, so this is lowest common answer So but the five is not equal to any of three or six Right five is not equal to three or six so what we're gonna do is we're gonna go to the left of five and We're gonna get So we're gonna go to left of five left five is one and three to the left of five is one, okay So here I'll just I'll show you guys what I mean. So let's do So now now what we're now we did it was we call it on the left side of Function call five is one right? This is all the left side five. So yeah, so now we have one We're gonna go to the top Is one equal to our star value? No is one. No, no as one equal to our destination value No, so then then we're gonna go To the left of one also. So we'll go to three. So here it's gonna call three now Okay, so now that we go through three Three, okay, so that's this part. So it's three equal to our start value Yeah, it is so then we're gonna return three so this is gonna return three It's gonna return three and then so once this return three It's gonna Go to the right of one right. Remember the right of one. So one's right is gonna be No, one's right is gonna be no so one's right is no They'll come back to the top here and it's gonna return all so then we have three and then no So three is gonna be on our left side and null is gonna be in our right side So here it's saying that if the left side is not equal to null and the right side is not equal to null Then we're gonna return the current root value the current know that we're on But here our left side is not equal to null. So we're just gonna return the left side So this is gonna return three, right? So yeah, the common answer three is just three So then this is gonna return three Okay, so returns three so now now that that this returns three We need to go to the right side of One. Yeah, so yeah, so that returned that so the whole thing returns three to the top And then we have to do the right side of five, which is two So what is two? LCA of two so this is like another recursive call Okay, and at two is two equal to our Star value or a destination value. Nope, right two is not equal to that So then it's gonna call on two's left and two's right So two's left is six and two's right is four. So it's gonna do LCA on six and then LCA on four Okay, six goes to here six is equal to our destination value. So we return six. So this is gonna return six Then yeah, then four Four is not equal to our Any of these so it's gonna recursively call four with both nulls and then if they're both nulls, it's gonna return No, no, so they're both the both these are null The left is not equal to no, so this is not it'll return right so this would just return null So that's it's four is return nulls. So four returns no, so then it's gonna go to no So then we have six and no Six and no if it's six and null, it's not even you return all so here six is not equal to no So then it returns six this whole thing returns six So then here we have three and six. Okay, so then our left side is three and the right side is six So both of them are not no Right, both of these are not null. So then it's gonna return Five Because five was the root when three and six were called when we're traversing Okay So, yeah Yeah, that's pretty a really fast approach of me going through lowest common ancestor I'll probably make another whole video about it later on About lowest common ancestor because that's a pretty That's an algorithm that is pretty important. Okay All right, so now that we got the lowest common ancestor Let's just go over how to get the path from our lowest common ancestor of a root to our start value and our destination value Okay, so here I pass in our lowest common ancestor of five. So Yeah, so here now that we have this we just need to Pass in five to each of this so So we have our lowest common ancestor five and then we have three and then we have six. Okay, so we have start and then destination So here from five to three now now what we're doing is we're gonna go from five to three. Okay So here start value search fries three. Okay, so is five equal to three? No is five equal to null. No So we're gonna recursively go on the left So it's gonna go down to the left So it's gonna go left down here and then what it's doing is it's gonna append L to the path So our path is now gonna have L Okay, so we went down here Yeah, so it goes back to the top now is one equal to no. No is one equal to our Three. Yeah, our search value is three. So one is not equal to three. So this does nothing also So then we're gonna go to left again. So and During that time we add an L to our path path plus equal to L so add an L So then this goes down again. So now we're at three We go to the back to the top Is three no, nope is three equal to our search value. Yep, three is equal to three So we return true. So then this goes back to here and then this is true Okay, so this returns true Returns true true. All right three is equal to three returns true now is Is our did we find it on the left side? So if we found it, let's just return true. Okay, so at this point, I just returned true for that So this is gonna equal just return true So we found it. So then at this point we do nothing because we already found it. Okay So yeah, now we found so we found three going through it Now let's go to the right side Not right side. Now we're gonna try to find six Okay, so Here so this one LL and return true. So this was going from five to three and it returned true L Okay, well we guys get what I'm saying All right. Now we're gonna do another thing We're gonna go from Five to six now because six was our destination. So let's actually get rid of the picture again I should go over topics like how to search through trees Okay, so now we're gonna go from five to six. Okay, so how do we go from five to six? Yeah, so it calls back here is Five equal to no nope is five equal to six. Nope. So we're gonna go through the left side So here it's gonna go to the left side So here right it's gonna go left side And add the now so adds path is gonna equal to L. Okay is So we're going to the left side here one L All right is one equal to six. Nope is one equal to null. Nope. So we're gonna go to left side again And we pass in L again, okay, so now our path is L Now we're at three. Okay is three equal to six. Nope is three equal to No, nope, we're gonna add go through the left side again passing L and the left side is null Okay, so now we're at down here. So this is that null All right is is null equal to null. Yes. So we're here at this point to return to false So this whole thing returned false which means we did not find out left side So now is found one equal to false is fine one equal to true. Nope. So we're done that now at this point We're just gonna pop back Pop back our path. So what that means is I'm not gonna go to the left side now from three I'm gonna go to the right side. So From this we pop this back from LL and then I'm gonna go to the right side So here we're gonna search through the right side of three. So here right side of three is null also So we go back here is null equal to null. Yes, so equals returns false So by the way, this is the right side three. So it would have been LLR So this is also false, which means we did not find it and then here would return False so here did we find out on the right side of three? Nope. So we're gonna pop back it again So we're gonna remove R again, and then we're gonna return false. So this is gonna return false again. So false so now we have both false's and this point at this point Yeah, it returns false so we come back up here I think it was on the note of one now because we went through the left side of three and we went through the right Side of three. So it's gonna go to one After it popped back again. So I think uh wait LL Hold up. Yeah, okay. I think it's gonna go to the right side now so at one it's gonna go to the right side and it's gonna return false again because There's nothing on the right side. It's also null. So then this whole thing it's gonna return null on LL. So here Going through the left side It's gonna pop back them twice again So because it's gonna keep going returning all so then at this point after every goes null like multiple times here It's gonna go to the right side of five because after searching through this many times popping back It's gonna go to the right side of five So when it goes to the right side of five, it's gonna go to the right and we're gonna add R to the right side of the path So now Let's just copy this It's gonna go to the right side of path. So here's right side of five, which is gonna go to R here and then from here We're gonna go to Did we find it? Nope Not yet, right? So then it's gonna go here. So it's two equal to Two equal to null. Nope is two equal to six. Nope. So now we're gonna go to the left side So here at from two so we're at this point. We're at two right because we went to the right side So it's two equal to null. Nope is two equal to six. Nope. So we're gonna go to left side so it's gonna go to left side here and Then from here it adds To the left side of the path here. So it's gonna add L here and it goes to the six So now at six is six equal to null. Nope is six equal to six. Yep So then from here It just returns true. So now that we have this the path is gonna be RL and returns true If it didn't return true, let's say assume that it didn't return true, right? It would have I would go to the right side of two again. So yeah, basically what it's doing It's it's going to the left and the right and left and the right until we actually find the node that we want We are searching for So that's that's basically what it's doing Yeah Okay, so once this is done it returns true. So now we have R and L for this part. So here get path of Of the first path of F is gonna be The first path that F is LL and then the second path of the path is RL so now we have F is gonna equal to LL and S is gonna equal to RL right F of F and S are like the paths of For five to three and five to six All right, so now that we're at here What I do is I just replace all the values of our path to be up because for the left side you have to go up right from left side you must go up because I mean to go from left to the Ancestor there's no way to go down or go right or so you have to go up So you go up and then it's what here what we're doing here is we're just replacing all the values From our F to be up. So it's gonna replace this with up and up and then now We just return F plus S which is gonna be up up RL. So up up RL and it returns that okay And after that, that's the answer and this code actually works it runs And yeah, that's basically the gist of how to do this problem. I hope you guys understand this video It's it was this is kind of kind of a long video to go over. But yeah, Raycom subscribe I'll check you guys later. It was a pleasure for this one But yeah, I'll probably go over lowest common ancestor next time. But yeah peace