 What's up guys? Mine's Michael and welcome to my YouTube channel. Today you know what we're doing. We're doing another league code grind, all right. We're gonna, basically I'm gonna zip through all 30 of the questions that I already answered. And after that, I'm basically going to do the questions with you while I'm doing them with you. And I hope you guys understand how to do these algorithms. Let's finish all the easy and then the medium and then the hard. It should be very, very easy for you guys. Algorithms should be a joke. Algorithms should be a joke. So today, we're gonna do the next question. Remember to go to algorithms, sign up on the code, the code, go to problems. Then later on, just click on algorithms. After that, once you put on algorithms, sort it by difficulty and then we will do the next problem. So we already did if you bang an IP address, we did using stones. Now we're gonna do range some of BST. So you guys remember what by the BST means by the BST, the binary search tree basically means all the values of whatever the current node you're on. All the values left of it is less than and the current value and all the values right of it is greater than the current value. So given the root node of a binary search tree, return the sum of all values of all nodes with value between L and right inclusive. Okay, so how do you do this? Well, first of all, we want to recursively call this function, basically. We want to basically get all the values that are between this L and this R. That's what we got to do. That's basically what we got to do. But while we're doing that, we got to recursively get it, right? Okay, so we have to sum up all the values that are between L and R. Okay, so to do that, first, let's think about this. I want to keep calling this function. How am I going to do that? Well, first, if the root is null after returns zero. Why? Because there's no value. Okay, if the root is null, right, that means that there's no value at that location. So that's why we're returning zero. Okay, there's no point of keep going for this if it's like it's not, if it's null, right? It doesn't make any sense. Because null means that there's nothing at that back. That's what we're trying to do. Otherwise, what are we going to do? Well, what we have to do is we have to call this function. Actually, before we call it, we're going to have a sum called sum. What is that to do? What is sum? Well, actually, that's not sum. We are going to... No, that's not good. I don't want to do it this way. Okay, what are we going to do? First, we're going to recursively call this function on the left side and the right side. Because I want to get all the values, right? I want to get all the values that are between left and right. In order to do that, I have to recursively call on the left tree and then recursively call on the right tree. To do that, I take this range sum, this range sum, and copy this. Here, in the roots, we're going to recursively call on the left. So I'm going to basically take in the left tree that I have here and then call this function again. This would get me the left side. What am I going to do it? I'm going to add it to the right string. So I put this, add it to the right. So what does this do? This gets me the sum of both the left and the right. So remember binary search trees, let me just quickly show you guys. Remember binary search trees? Let's say this is the node and there's children right here, left to left. If I recursively call, basically, I want to call this function and get all the range sums, right? So in order to do that, let's say this has children, more children right here. I need to keep calling this function. I want to get the left, left children, its sum added to the right children. So I'm going to keep doing that. So what we're always going to do is let's say this is the parent and we're going to get the left children. Sum adds to the right children's sum. But what is the left children's sum? Okay, that means that they go down even further. What is the left children's sum? This one, add it to the right children's sum. That one. And then it returns this and it does that, keeps going back until I was given. Get the sum back to the point. Now, once I get the left and right, I need to check if the roots value is between left and right. So if it's greater than or equal to left and roots value is less than or equal to right. Why? Because if I do that, I'm going to add it to the sum. What would this do? Basically this would be, this is going to take the value, this is going to check whatever value I'm on. So let's say whatever node it is. So let's say, in this example, let's say 10, 5, 15, 3, 7. So let's say it's going to be 10, 5, 15, 3, 7. What is it going to do? Basically it's going to recursively call and they get everything of the left child and the right child. So what's the sum of all the left child? Well, so it's going to go here. Be like, okay, what's this little left child? It's 5. Give me the sum of 5. What is that? What is the sum of 3? I don't know what the sum is 3 because the sum of 3 is going to have 0 or 0 when it's not, right? Then once it returns 0 up here, it has to check if the current value 3 is between left and right. So if I were to call this function with left 7 and r15, it's going to check 3 is 3 between 7 and 15. So it's not going to add it to it. It's not going to add it to it. So it's not going to return, so it's still going to return 0 here, right? Then that would get me the left sum. Then I'm going to add it to the right sum. Get the right sum. I go to the right and I check if it's 7 between 7 and 15. It is, so I return 7. Then once I back up here, it's going to return 7 and plus check if it's 5 between 7 and 15. 5 is not, so I'm going to return 0. 0 plus 7, it returns 7 here. And I'm basically the same thing on the right side. So with that, then we're going to return sum. And after that, this should ideally work. Yep, you got 3. Okay. And then I'll submit it. Accept it. This is faster. 97% of C-pup is all right. So that's basically how you would do it. Let's see how they do it. Okay, so that they do that for social. So that's basically what I did. Oh, so here they added an output function. You technically don't have to do that, but okay. Oh, so they check it. It's not here. And then they add it. If 5 is between left, it would be 6.5. If 5 is between left, it would be 6.5. If 5 is between right, it would be 6.5. So we basically did what we did. The other way you could do it is iterative method, which is using a stack. So what they're going to do is they're going to keep pushing whatever current value is onto the stack. And then while it's not empty, we get the top, the first node of the stack. So here, let's say we have a, so let's see how it works. Okay, so we push 10 onto the stack. We push 10 onto the stack. And while the stack is not empty, 10 is not empty, right? So then they're going to take 10 out. And 10 is out. If you check, is 10 not equal to no. Yeah, 10 is not equal to no, right? The node at 10 is not equal to no. So they're going to check if the value, the node value is between left and right. So it's 10 between, let's see, what are we going to call it? What are we going to call it? Oh, seven. Okay. So here, left. Okay. So we're, what do we call L seven? And then right is 15. Yeah, we're calling the function L seven. So over here, if it's between it, it adds the value to the, adds whatever we popped up in the stack, adds it to the answer, which is, apparently it's zero. So it adds it to it. Then after that, we push the left node. If it's left then, last one, we push the left node. So what is that five? The value of five is less, is greater than left. We push five, and then we keep doing this. And then we check if our value is less than right. Here. Then we push it. And then we keep doing that until we've finished with that. And that's how it works. That's basically range sum of BST. I hope you guys enjoyed this video. Great comment. Subscribe.