 What's up guys, Mike the coder here today we're going to talk about is this a binary tree a problem on hacker rank. So the only thing you need to understand about binary tree is that basically we're writing a function and we need to return whether this function is this tree is a binary search tree or not. So what you need to understand about binary search trees that everything on the left side must be less than the current node and anything on the right side must be greater than the current node. So as you could see in this picture, which I will now go to here, this is not a tree. It's not a binary search tree. And the reason why is because this right side is not greater than three, right? So two is not greater than three. This six maybe be greater than three, but because two is not greater than three, it's not a tree. Also five should be less than three. This node on the left side of this three must be less than it. Okay, it cannot be greater. So that's why this is not a binary tree. And yeah. So yeah. So what you could do to solve this problem is you could try to check is the left side of your is a left node smaller than your current node. And is your right node greater than your current node, right? Because if they are both true, then you have to keep checking, right? Now is if your left node is greater than your current node. So if this is greater than or equal, then it's definitely not it, right? And if the right node of two, if it's less than or equal to the your current node, then it's definitely not it. So like, so like, what you need to check for is essentially check for is your left node greater than or equal to your current node, as your right node, less than or equal to your current node. And if that's the case, then it's not it's not a binary tree. Now there are some edge cases you need to check also, you need to check if this whole subtree, this has whole thing has to be smaller than your current node. So if this if any node in your left tree is greater than your current node, it's not a tree. So you also need to check if this whole subtree on this side, is it if it's less than your current node, right? If it's less than your current node, it's not a tree, right? All these values have to be greater, too sick, all the values on your right side must be greater than three. So right side must be greater, must be greater than three, left side must be smaller. This must be smaller than three. It must be smaller than three. So that's essentially that's essentially the gist of it. Left side must be smaller than the current node. Right side must be greater than the current node. If any of these are false, it's not a tree. So yeah, the only issue is that you have to check all the values here must be smaller than the current node, and all the values here must be greater than your current node. So that's a little difficult to do it recursively. I'll show you my code now. So hard to do it recursively. But if you were like regularly, yeah, okay. So first of all, let's actually check first. So first of all, we need to check is the left node that I created to help her functions called check left and check right. So the first one is check. First one, check BST of our function, what I'm going to do is I'm just going to return is the left check possible and is the right check possible, right? So left check is checking, are all my values in roots left less than the current roots data? Right. So all my values on the left side must be less than the current roots data must be less than this three. And that's what this check left is going to check. Is all the values on the left side of my route is it less than the roots data, the current roots data? And if it is, this is going to be true. Check right is going to check are all my values on the right side, this right side, are all these values greater than three, they have to be greater than three. So I'm going to return roots. I'm going to check right, pass in the right side of the tree and roots data. So all the values on the right side of tree must be greater than roots data. And I'm returning both of these and I'm adding them because they both have to be the case. They both have to be the case where the left side is smaller, all the values on the left side is smaller than the current root. And all the values on the right side must be greater than the current root. So that's why I have and then together. So now we're going to check, we're going to explain what check left does. And it's a little confusing, but bear with me. If you what we're doing in check left is we are going to recursively call this function. Okay. If I guess if when we reach a null pointer, which is going to reach the end of something, we're going to return true, because this is the base case. When we reach the end, if we keep going to the left, let's say I keep going to the left, I'm just going to keep going, keep going, keep going until I reach this one. And then I'm going to keep going again. And then I'm going to hit null. So this is going to be like, empty null. If I hit null, pointer, I just returned true, because that means that, hey, if all of it worked out in the end, yeah, I'm going to return true. Now, if any of the roots data, any of the data that I hit, like if any of the nodes that I hit along this way of reaching to the end. So if I hit any of these nodes, any nodes, either this node, this node, this node, I hit any of them, right? And there are any of them are greater than this data, greater than three, our roots data, then it's not possible, right? Because what we're doing is that we need to make sure all the nodes on the left side, all the nodes on the left side must be less than the roots data has to be less than three. So if any one of these that I hit is greater than three, if any of these I hit is greater than three, then it's not true. Right. So we got to return false. And any of the values that I hit, any of my roots data is greater than or equal to the data. I'm returning false. Now this is a little tricky. These calls are going to be a little tricky to understand, but bear with me. We need to check the first one is a recursive call. So I need to check is roots left? Check left, right? Is it roots left less than or equal to roots data? So this is what's what we're going to do is if this if this node is the case, right? This is not this node is not the case currently, but let's say it was less than it. Let's say this was like, I don't know, let's say this was like two, okay, let's say this was two, let's say like for sake of the argument, okay? What this is going to do is we're going to, if this is the case, right, this, this if statement where is never going to hit, right? Because we know two is less than three, right? It's two is not greater than three, two is less than three. So this statement never gets called, right? This statement if statement, so it doesn't return false. So what is it going to do? Check left. We're at check left, right? It's going to recursively call on the left side, making sure that the left side, this side is also less than three. So we're going to keep going down to the left side until we reach to the end node, checking make sure all the values on the left side, all the nodes down here are less than our current root of three must be less than, okay, that's what this if statement is. Now, what else do we need to do? We need to recursively call and check, check left roots, left data is less than the current data. So what this is do this is a little tricky to understand, but you got to bear with me. There are some cases where it would fail. But what we need to do is that not only are we checking is this node less than three, right? So this first check, check left to make sure that all the values on the left side is less than the current three, but we need to also check are all the values of this side less than our parent group, right? This side has to be less than the current parent root. If we have a tree like 657, right, and then this side is actually 10, the left side. This would return false, right, we need to check. Is this side also less than five, okay? Is this left side also less than five, because all the values on the left side has to be less than your current current root. Also on the left side, it always has to be less than it. So what this second recursive call is checking is is the roots left less than data, less than the current data. Okay, that's what it's checking for is my left side also less than this current data. And if it is, it's going to return true, if it's not, it's going to return false. This part is going to get even more tricky to understand. Like left roots, right, roots, right, Ada, okay, let's say roots, right here. This is five, and this is 10. Okay, this is not a binary tree. Why is that because this 10 is greater than six, right? Remember all the values on here has to be less than six. All the values have to be less than six. So if we have 10 here, it's not possible, okay, because this 10 is not less than six. This 10 has to be on the right side, right, because any value that's greater than the current root must be on the right side. So that's why what this is checking for this check left roots, right data is checking is this right side, smaller in our root, whatever I'm currently on this part. The left side is right side, is it smaller than the root? If it is overturn true, if it's not, it returns false. All right, this is a little, little hard to understand also check right, roots, right, roots, data. This is check. So essentially, check right is another function I wrote. It's the same thing, same exact same concept, except check right is going to make sure anything on the right side is smaller than or equal to your data. So on this right side, if it's smaller than your current data, they are on it's false, not possible, right? So check right is making sure that all your values on the right side greater than your current data, all the all greater than it. So on for under this part, this check rights statement, what we need to do is we need to make sure the check right of the right side is going to be larger than a roots data, because if it's not, then it's impossible, right? Check right, the roots right side is greater than has to be greater than your root current roots data. OK, so check right. Now we have check right, check right is exact same concept. What it's doing is if the roots null pointer, we return true. If it's the roots data is less than or equal to data, return false. Check right is going to make sure that. We have root roots, roots, right, roots data, check right, check data. It's going to make sure the right side of the right rights of roots, right side is going to be greater than the data. And then it's going to recursively call on the right side, making sure the right side of the new other is greater than its data, like the current data that we passed in, right? And it's also going to check recursively call on the right side is the left side of its data on the right side, greater than the data. So like if, if we're on, if we're on like seven, and this was like two, right, this is not possible, right? Because this seven's left, two has to be greater than six. Two must be greater than six. Anything, all the values here must be greater than six. So two cannot be possible, right? This two cannot be possible. It's impossible because this, this, this side must be greater than the current's root of six. All the values on the right side must be greater than six. So this two is not possible. So what this is checking is, is that check, right? Is the left side greater than the data? So is the root, the side on this date, this data, is this left side? All right, this left two is a greater than the current roots data. Okay. And that's why we've got to pass on the roots left, this left and the data of six. So that's what this check right is. Check left is just making sure the roots left side is less than the current roots data. So like they're checking is, is that, is this left side, let's say we have seven and this was like, I don't know, let's say this was 10. What, what this is checking is check left, we have to make sure sevens left must be greater than seven, less than seven, right? The left side of seven, this, this nodes left must be greater than seven. So this is not possible. Right. That's what we need to check on the left side, because where the sevens left also is a binary search stream, it's also has nodes and values in it that are smaller, anything that's smaller than the node current node is on the left. Okay. So that's what this is checking and check left, left, you know, and then we end them both and yeah, we just return. So that's a whole gist of, is this a binary search tree? Hope you guys enjoy this video. Right. Come subscribe. I got a head out and yeah, peace.