Hi, everyone.
So in this video, we are going to solve this question, convert sartorially to binary search tree,
so given us today.
So if this is my area, these are the elements one, two, three, four, five, six and seven.
So this area is basically a certain area.
And what we need to do so with the help of these elements, we want to construct a binary such tree
so this can be one of the best five, six and seven.
So this is the root node and these are all the nodes.
So this is a bisdee.
So we can construct this.
But what I want to do, so I want to construct a balance to be housed, so basically for distri, so
for these elements, one, two, three, four, five, six, seven.
So how I will construct my body so I will find out the main elements.
So this is the main element.
I will make the main element as the root.
So four will be my root.
So all these elements will be the part of the nursery and all these elements will be the part of the
right subtree.
So for the left subtree, I have elements one, two and three and four, the right subtree, I have
elements five, six and seven.
Then what I will do, so I will call education for the next appre and I will call occasion for the right
sempre so far left Sabry again I will find out Dimmit so I will construct the node.
So this will be delivered subtree and this will be the right Sabry.
So when will when will be on the left and he will be on the right.
Similarly I will.
So this is the main elements of this will be my route six is the route for left.
I have five.
So this is the left and this is the right subtree.
So seven will be in the right.
So finally, virtually my balance to Boudi.
So this will be for then I have two, one, three, six, five and seven.
So this is a beast and this is also a beast.
But I want to construct a balanced beast.
So this is the right answer.
This is the right answer.
So how we can solve this question.
So it's very simple.
We will create a function.
Now, that function which will take it, will take that as input.
It will take this starting.
It will get started next.
It will take the index and it will return the node.
It will return the route in order.
So North Star will be needed and type what we will do.
So we will find out the more relevant it will find out, the relevant.
I will construct my route.
Then I will call into question for the left subtree.
I will call the collision for the right some pretty simple.
So this question is basically present only called.
So this is the question.
So this is the question I need to convert, I saw today to a binary search tree and it sees your tree
should be height balanced.
So.
We are taking it as input and we need to return the route, so as discussed here, if it will see.
So we need to construct a function like this, a function that will take every state index and index,
so let's write the code so we will create a helper function.
So let's create a helper function to train all star helper, so what it will take, it will take as
input.
So let's say the name of the area is the name of the vector is Eddie, oh, let's name it, only legitimate
error, it will take what is the starting index of the array?
And it will take a what is the end index of the Eddie?
So how I will call this function so intense, how many number of elements are present num start size?
Then I will call this function helper.
So return call the helper function, give it a.
So I am passing everybody day, starting next, starting next zero and indexers and minus one.
So now we need to write the code for this function helper, so first of all, we need to discuss what
should be the best case.
So these cases, if there is somebody.
So for MPRDA.
For them to start will be good and end.
So in this case, for them, my body should be null because if it is empty, then there will be no elements
by necessity, so you will return otherwise what we need to do.
So as discussed the approach, what we will do.
So, first of all, we need to find out.
So if these are the elements, first of all, I need to find out the main element.
So I have this starting next.
I have the index.
I will start by two to find out.
So what is the start plus and why do so?
After finding out, I will construct the node.
Then I will call for the left subtree.
I will call Dacogen on the right subtree so far left.
Sabry, what will be your next year and next will be of minus one and for calling on the right subtree.
What is your starting next.
So starting next will be my plus one.
Simple.
So this will really start the next four calling on the right three and this will be the end index for
calling on the left.
Sabry So let's do that.
So first of all I need to find out the main element, main index.
So many nexxus start plus and vidoe.
And what is my data.
So my roommate is.
Area of.
Now, after finding out the data, let's construct the root node, so the North Star route.
Is Nutri A..
And I will give the data so I yield data.
And now what we need to do so we need to call the helper function for the left subtree.
So called the herbal function for the left subtree, I will give it the starting index is start and
the end index is basically made of minus one.
Similarly, we will call the helper function for the question on the right subtree for building the
right subtree.
So START index is made plus one and index is same as end.
So let's give space here, so starting next, start and end indexers minus one, and then what we need
to do so after constructing, we will return NEEDWOOD But there is one problem with this code.
So what we are doing at this line, I'm constructing my note, so I am constructing my note.
Then I'm calling the helper function.
So I am passing the starting next and made my and so I am passing the left separate.
So what will happen.
Left subtree will be constructed and we will return the so that the function is rude.
Then we are calling for the right subtree.
We are passing start Izmit plus one and we are calling recursion collision that will be constructed.
Right besty will be constructed and we are returning the rule.
So what we need to do, we need to make this connection.
We need to make this connection, so this is returning root node, so let's make these connections.
So making the connection, we will right the first time building the So Rudra left.
Call the helper function, then I'm constructing the right subtree.
So now I think this is correct, so rude or left, call the helper function at a start, minus one rude
right.
Call the helper function at a.
And so I think our call will work.
So first we will run our code and then we will submit.
So let's run our code.
Now, let's admit.
OK, so basically our goal is working, so this is from this video, I will see you in the next one.