Hi, everyone.
So in this video, we are going to write the court for this problem and let's start writing the code.
I have already explained you everything.
So now the only thing remaining is to just start writing the code.
So, first of all, we should change the name to in order.
Right.
We are given in order areas input.
And let's discuss the recursive relation once again and let's write it somewhere.
So we discuss that.
First of all, we will find out the maximum element in the area and obviously the index for this and
let's call it mixed index.
We will find out the maximum element and the mix index.
So what we'll do after finding out the maximum element, this will be my root node.
This will be my node for calling on left.
I will pass this index and I will pass.
Max index minus one.
So this is the START index, this is the end index and for calling on right, the START index will be
mixed index plus one and an index and that's it.
And that's third rate.
So let's start writing the code.
So first of all, let's check the size.
So if the size of the area and the size of zero, that means the air is empty.
So in this case, your tree will not exist so you can return null.
Otherwise, what I'm going to do is write a function helper.
So what this helper function does, it creates three.
And I need to pass the in order that I will pass the starting, which is zero starting index and I will
pass the ADD index, which is in order that says minus one.
Fine, this is the START index and this is the end index and this helper function, it will return the
routine order of the Nutri.
So what this helper function will return a dilettante root node.
So it is a train or start helper function, what it is taking as input, it is taking in other areas
input so you can take in order that is in port.
What else.
So it is taking the starting next.
It is taking and index as input.
So this is going to be a recursive function.
So we know the recursive relation, but what will be the best case?
So basically it is very simple.
If START is good, then this is a start.
So if start is good, then end.
It started as guerdon, and that means they're done.
We will return null now.
What we need to do, so what was the first step?
First step was basically we need to find out the maximum element, because that will be my route to
find a maksym element, because that element is my route.
So let's take one variable, let's say the maximum value, so what is the maximum value?
This is in the minimum and obviously we need maximum indexed so that I can call on left and right.
So let's take one more variable, which is maximum depth.
And let's say initially this is minus one.
Now we're trying to do.
We need to iterate over the area to find out the maximum element.
So I will start from starting index I list in articles two and index I placeless.
So if the current element.
Is greater than the maximum value, then we are going to update the maximum value to this value and
obviously we will update the maximum index.
So Maksym indexes, I find.
So now we know the maxim element so we can create root node, so let's create a root for the Soul Train
Wildstar Root will be new right node and we need to give the data that is the maximum value.
Fine.
So now for this route.
And to find out the left subtree for this and we need to find out the rights of this and after finding
out these two values, we will return.
So what will be left?
We discussed that.
What I need to do, I will call on left separately.
So in order, it will remain same.
Starting next will remain the same and what will be the end and will be the maximum index minus one,
and for building the rights of Prevert, I will do the start.
Index value will be the max index plus one and and index and the two guys that said, so what we are
doing here, this is simple case, right?
So our approach was, first of all, we need to find out the maximum element late because the traversal
is left-to-right, the maximum element will be our root node.
And obviously we need to find out the MAKSYM index so that I can call on left and right.
So I am migrating from start to end.
And if this data is greater than the maximum value, then you will update the maximum value and the
maximum index, which is simple right now.
What they are doing, since we know the maximum data, let's say the maximum value is five.
So I'm creating a node with the data five.
So we will create an order of data five and four, building the three.
We need to build the left of the tree.
We need to build right on the tree.
So I am building left and building right for building left called the helper function for building right
called the helper function and the end will be maksym index minus one and the start will be in this
case maksym index plus one.
And that's a very simple code.
So let's try to run our code first.
We will test against the basic test cases, then we will submit.
OK, so the spelling mistake, this will not be try this is actually three.
Yep, so let's admit now.
So our goal is working, right?
This was very simple called.
Only this much amount of gold, so this was really, really very simple.
So this is all about this video, guys, I will see you in the next one.
Thank you.