 What's up guys my name is Michael and welcome to my YouTube channel today You know what? We're gonna I'm gonna be spamming a lot of these videos because I want to make sure that we could get to leek on medium Before the end of the summer. So you probably see 15 videos uploaded today. Just try to get past All the easy's you know, okay? So today we're gonna do search in a binary search tree. So what is searching a binary search treat? Given the root of a binary search tree BST and a value you need to find the node in that BST that the nodes value equal to the given value if the Subtree rooted with that node. Oh, yeah return the sub tree rooted with that node If it was such a no doesn't exist you should return all this is actually pretty basic Remember how binary search trees work everything on the left side is less than the current node and Everything on the right side is greater than the current node. So here we have four everything left side is less than two is less than The all the values on the left side you see one two three That's then the current node is four and everything on the right side this seven is greater than a node current node at four As you can see here in one two or three For the node to everything on left side one is less than the value two and everything on the right side Which is three is greater than the value of two so now please Try this problem yourself and then come back to me when you figure it out Okay, so now I'm gonna try to go over this problem with you guys So first we got to check if the root is null Because if the root is null No, no if the root. Okay, think about it. It's root. We got to check if the root is not null Reason why is because if the root is not null then we could then we could search Otherwise, we can't really search right because if the root is null then I mean Yeah, you can't you can't really search anything so If the roots not null then we're gonna check if the roots value if the roots value is Equal to the value we're searching and if it is then we're gonna return True, okay, not true. We're gonna return a root. Okay? Okay, now we got to check if the roots value Actually now we got to check if it's less than then we return But then we're gonna go to the left side and check that and if it's greater than we go to the right side and check it So here we're gonna do that. So let's check else if roots value is Less than value Then we're gonna return search BST. We are going to go to it We are going to go to the left We're gonna recursively search to the left side of the tree and if it's greater If the so if the root here is greater Then the value we're searching we're gonna search to the right side of the tree and I think that's pretty much it After this we could return null because that means that Essentially that means is that Well, then there's nothing there. So let's run the code Think this is it. I could be wrong though. I haven't done this in a while like I fit. I did this problem way back. Okay, so Whoops. Okay. What did I do wrong? Let's see. So this actually outputted nothing and it expected to 1 3 Okay, oh, let's see. Do-do-do-do-do-do-do. What did I do before? Let me see How to accept it before though quick quick look. Oh Okay, it's actually value My bad. So, okay, my bad. I switched it Okay, so it's actually you don't actually want to check the roots value is less than value You I swap these by accident. So you actually want to check if So if the value So for the value that we're searching for Let's say here. We're searching for two, right? So Let's say we're searching for two. Okay, if the value of two, which is two is less than the roots value four Then we have to search on the left side. Okay? So I mixed it up see it like to look in this example if we're searching for the value two The our value that we're searching for is two right and our current root The current node that we're on is four since two is less than four Then we search on the left side so that we should go to the left side Okay, and then here they'll find two and return it. So I accidentally mess this up So I'll explain it again just in case if You guys don't understand it okay, so We run it. Okay, so now it's right. Okay Let's submit it. Okay. So now I got accepted So essentially is let's say I we I want to search for value Five or let's say not five. Let's say in this example. I want to search the value seven if The value we're searching Seven is greater than the current value that we're on for we got to check the right side Okay, it's not it's not the root that we're That is greater than it's a value that we're searching for so if I'm searching for seven and I realized seven is greater than the root. I'm on which is four seven greater than four I got to check on the right side of four So this part this the right part and I'll find that seven is Equal to seven it'll return this seven So that's why it's actually value that is less than the roots value than search on the left side If the value is greater than the roots value we search on the right side Okay So these are the codes is the code that helps you search it. Okay That's essentially how to do this question great calm subscribe. I'll check you guys later for the next video. Peace