 What's up guys my name is Michael and welcome to my YouTube channel today We're gonna do another leco challenge called converted sorted array to binary search tree So you're given an array where elements are sorted in ascending order and you have to convert it to a height balanced binary search tree So height balanced binary search tree is where the sub trees are never differed by more than one So guys, um, I know I haven't been uploading a video lately for a lot But I'm gonna get back to it. I'm getting back into the grind of uploading daily videos So now every day I'm gonna upload at least at least one video every day. But yeah Yeah, I'm not gonna upload like at least 10 I'd like 10 in one day, but I'm gonna upload one at least one leco challenge every day So Back to this question converting a sorted array to binary search tree So how do we convert a sorted array to binary search tree well one way to do this is to every time you We would take the middle of our list So we go through it. We have a list of values negative ten negative three zero five nine One way you could do this is to take the middle value of your list and have that be the root Okay, so in this case negative ten negative three zero five nine This is now we're gonna have the root as Zero, okay, and that would be a root and then our left the left and right would be The left and right would be the next middle of the left the left side so The left and right sub parents would be actually the middle values of the left the left array which is whatever on the left side of this side and Right array, whatever the side on this side So I'm not gonna create their tree like this But I'm gonna create the tree some some somewhat similar, but it's not gonna be it because they say they just want a following balanced binary search tree, so What I'm gonna do is here you have negative ten negative three zero five nine, right? so here Let's see negative ten negative three Zero five nine So if you want what I'm gonna do is I'm gonna take the middle value. So this is gonna be my middle Zero and I'm gonna make this the root of my tree my new tree. That's supposed to be a node. Okay. Let me redraw that So this is gonna be my new tree value zero and then I'm gonna have a sub sub values on the left and right and The left tree the left left root is gonna actually be the middle of Whatever is on the left side the rest of the list on this side So my root of the left side is gonna be the middle value of whatever is less on this side and then my right side Would be the middle value of whatever list on the right side so in this case the Middle value so zero one zero one two zero one zero plus one is gonna be Zero plus one is gonna be Let's see Minus one so yeah, okay, so zero plus one is gonna be one divided by two would be one So that this negative three would be my root Of my lower value Yeah, that's what it would be and then the same thing would be on the right side five and then nine so Zero, so this is the the index starts at zero one two three four, right? I'll show you the indexes. I'm gonna choose them in the middle value of my right my right side, which in this case is Five five nine whatever on the right side, so if I want a middle value I'm gonna take the indexes and add them up and divide by two so three plus four is Seven divided by two what seven over two. Let's see. Let's quickly do the math Three yeah, it would be three because we round downward and What's three's index zero one two three three the index of three is five so that will be my next tree my next root and then On the left side, I'll do the same thing again, and I'll pick the next value Which would be the root of that value and you'll get negative ten here If you keep going and do like that you get negative ten and then you'll get nine Basically you would get nine and then you have like no values on the side, but yeah, that's how I would do this so You I would just pick the middle value The left side and then whatever value middle value on the right side to be my root because that would that would make more sense Okay, so how do we code this? We're gonna create a method called construct that takes in We have a method called construct This is gonna take in the the start value and then the end value which is gonna be our the starting and for our Constructing our Tree that we're making okay, then what we're gonna do is This start and end this construct method would actually return a tree node and That's gonna be the tree node of That we're gonna return We also have to pass in the whoops my fault My insert was on okay, we have to pass in the Numbers that we're passing in because we need to construct it so what we what are we gonna do? Well, if start is greater than end We're just gonna return null Because what we're gonna do is we're gonna create a We're gonna recursively call this method on the left side of middle and the right side of middle And we're gonna get the middle middle index every time so Starts greater than and we're gonna return all Otherwise we're gonna get the middle index, so I'm gonna do middle Middle index is gonna start plus end and then I'm gonna divide by two Okay, so now what I'm gonna do is I need to create a new node that has the Value at nums at start so I'm gonna create a new node country node a New node and it's gonna have the value of nums at middle index Okay Then I'm gonna do new nodes left child is going to It's left child is gonna equal to I'm gonna recursively call this method on the left side We're gonna be point at whatever's the new node has returned on the left side And then I'm gonna do the same thing on the right side And I'm gonna return new node. Okay, so now what is gonna be on the left side? Left side is gonna still be nums except now we're gonna Our starting values and ending values are gonna be different. So our starting value is going to be Zero to middle minus one because that's gonna be the left side of our tree my bed So that's gonna be remember the middle zero Now we're gonna create recursively call on the left side and that that left side is going to be Whatever our middle value our middle index Right the middle next subtract one and I'll be this and end value this So this is gonna be yeah my bed middle index and Then this is gonna be Middle index plus one to And this has to be the size of the list Yeah, okay, it would have to be the size of the list Okay, then what I'm gonna do is I'm gonna just going to return construct and pass in the nums Start will be zero and Would be nums dot size Okay submit the code Compiler, what is the problem? Let's see? Base pointer has non pointer type tree node. Oh, okay. I actually have to create a new tree node with this Okay, um So what I'm gonna do is I have to use a new keyword Equals a new tree node It's gonna be a pointer to a node and Then they'll they'll fix that because then I'll get left side. We're actually better if I just use the dot operator It makes more sense But I think this is fine. Anyway runtime error. What is the problem stack overflow? Okay, let's see Let's see what the issue. Okay, so I Actually meant to do size minus one because that's actually the ending of where you're gonna do This is should just be end Because here we're constructing our a using size minus one Because a construct is gonna the end value is gonna be the length of the vector subtract by one Right because it can't be the size of the vector right here here The ending is it's not always going to be the length of the Array our ending is actually going to be recursively called so In this case this ending is actually going to be Here and in the middle value from zero to middle minus one right here. It's actually gonna be zero to Middle plus one put to the end right and could end could be whatever and could be changing So that's why it's not it's not always gonna be num size minus one All right, we'll submit that and then see What occurs wrong answer, okay, let's see. What's the issue? Do do do do wrong answer negative three. Okay, so we're getting a lot of nulls. We're getting a lot of nulls. Um, let's see Okay, so the problem that we had before is that it's not always gonna be zero to middle index minus one it's actually just gonna change based on start because Start could it's your your partitioning your recursively calling on the left side and the right side and that could change based on Whatever value that's starting. So that's why this this value should be start and this value should be end. Yeah Okay, I'll submit it and then this should be working Yeah, okay, and that got accepted. So that's how you do it. He would convert a story right to a binary search tree Let's see other people's recursive Solution, let's see this guy's Let's see how they do it Okay, so that's basically what we did Instead they return zero. That's that's funny Yeah, okay, that doesn't What this okay, so yeah, that's basically what we did They checked if the size equals zero then they do know okay Let's see. There has to be another one. Oh, this one's faster 94 percent. Let's see what he did. Oh Wow, okay, so he actually did a Okay, okay So what he did was he created? If start is equal to end Start plus and plus one over two Okay, he gave the he created the same list If it's not he were sets root as and then he adds the tree Okay, so he does the same thing start Index minus one start plus one and Okay, so that's literally the same thing as what we did. Uh, what is I think this is like read This code is just like we changing the Tree to make sure it's balanced. That's what this code is doing. But yeah That's how you would do this solution rate com subscribe. I'll check you guys later on next video. Peace