 What's up guys? My name is Michael and welcome to my YouTube channel. Today we are going to go over binary search tree lowest common ancestor so essentially, we're given this binary search tree and We're given two nodes and those two nodes are v1 and v2 And we want to find the lowest common ancestor of v1 and v2 So what does that mean that means that it's whatever is the lowest node that is shared by both of the nodes So if we look at this picture of one two three four five six If I pick four and six right as my the two nodes that I want to find lowest common ancestor If we keep going up from six What is the lowest node that is going to be shared right if I if I start from four and I start from six I keep going up So six I go up as five and I go up and I get to three And if I if I'm at four and I go up I get to three So the lowest common ancestor of four and six is going to be three because if we keep going up from Four and from six we're gonna reach this ancestor of three We're gonna reach this node that it has that is shared amongst both of them so that's basically what lowest common ancestor is and if we have this node of Four two seven one three six this binary tree and I ask you what is the lowest common ancestor ancestor of one and seven Essentially if I keep going up from one I get to two and then I go up from two I get to four and from seven if I go up from one I get to four So lowest common ancestor in this case is going to be four and the reason why this is the case is because So if you keep going up and up and up they're gonna reach the same node of four So the code here is actually not super difficult if you Essentially is is that if you Basically, I'll just I'll just explain the code. I'll just explain the code for you guys because it's a Essentially what we're doing is that What we're gonna do is if the left-hand side of the left node is greater than the note the current route and The right-hand side is greater than the current route We're gonna go to the right If the left-hand side is less than the root and the right-hand side is less than the root that we're gonna go to the left Otherwise return root and why does this work? Let's look at this we have one two three four five six right and We at we're at four and six as you could see here lowest common ancestor if you see Anything that's shared right it means is that the The left the left node of four. It's on the left side of the node of the lowest common ancestor of three Right it's on the left side and the right node of six is on the right side of three So essentially what we're saying is that if we reach a point where we're traversing down that these nodes, right? and the left-hand side of the root of the node of V1 is actually to the left of the node that we're on and the right-hand side of v2 is on the right side of the Node we're on then we return because that that's essentially that means that we're actually at the lowest common ancestor So I'll that's actually going to go over this example. So let's I'll just copy this image and Go over the example with you guys here and then I'll show you guys the code I'll just copy the code real quick So you guys know what I'm talking about Dang, where did it go? Actually, this is not a good. All right. This is the code Recursive code and let's go look at the picture So let's have a pic four and six Right. I'm gonna pick four and I'm gonna pick six. This is my v1 v2 What is the node gonna do if it when this code is called? Okay When we're on the root of two it's gonna check is my four greater than my current root and my and six greater than the current root right because if both of these nodes are on the right side or The left side Right, if it's either on the right side or the left side It means it's not the common ancestor Right because we saw that earlier that v1 If it's lowest common ancestor that means v1 is on the left side of the nothing and v2 is on the right side of The lowest common ancestor. So as we could see here if we're starting at the root of two Is v1 greater than and is v2 greater than it? Right it is right four and six are greater than two right before is greater than two and Six is greater than two right? I can't type that but four is greater than two and six is greater than two So that means that two is not a lowest common ancestor because four is not on the left side and six is not on the right side Do you guys follow? So that's why we're gonna recursively call on the right side and we're gonna check this side because we know that Four and six are greater than two. So therefore the lowest common ancestor have to be on the right side Now we're recursively calling here on three is Four and six greater than three No, right four is not Actually, let's see is four and six greater than three. So let's let's see. Okay. So is four greater than three? Yes, four is greater than three wait wait wait hold up This note is not actually possible this note is not possible This four should be on the right side. This is not a possible tree because This four supposed to be on the right four is greater than three Yeah, this is not a possible tree. That's a really dumb dumb tree Okay the reason why it's not possible because Anything on the right side is supposed to be greater than your current node and four is greater than three But four is on the left side. So this tree is actually not even possible. Let's actually not use this example Let's use this one But I did explain what you guys what I was talking about though But essentially is is that if let's say let's say we're on this tree and we want to find the LCA of 147 so one and seven So actually is is that if they're both on the On the right side of the current node, then we know it's not possible Right that this is no four cannot be the lowest common ancestor So we have to recursively call the right side if they're both on the left side of the node right if they're both on the left side, so let's say Let's say we're instead of trying to find seven. Let's actually try to find three, right? So let's say one and three are both on the left side of four So we know that four is not possible because they're both on the left side. So this can't be the lowest common ancestor So then then we're gonna recursively call on this side and we check is one and three less than two It's not right one of three is not less than two. So that's why it's gonna return to this has to be the lowest common ancestor The reason why is because if one is on the left side and three is on the right side That means this is this must be the case of the lowest common ancestor Because that's how the that's how this thing works, right? That's how the definition of it works So yeah, it's actually is is that if we are at the current node We're gonna go through a curse we call the current node If both of my nodes that I want to find the lowest common ancestor are greater than the node Root of the node then the current root cannot be it and I have to check the right side Because right side will have the values that are greater, right? If they're both less than the current root, I'm gonna have to check on the left side It means that the current root is definitely not it So I'm gonna recursively call on the left side and find all the values at a left Then the root to check for the lowest common ancestor They're not either this case. So if they're on this this the sort of form where one is on the left and three Is on the right All right, three is greater than two and one is less than two then this must be the lowest common ancestor So that's why we return root But yeah, that's essentially the bit gist of how this this problem works. Hope you guys know this video rate com subscribe I know I had to like not use this example But this example is not just not possible because four cannot be on the left side of three because Did they even talk about this? Okay, whatever I didn't explain it But yeah four cannot be on the left side of three because I thought it violates the BST property But yeah, great cop subscribe. I'll check you guys later. Peace