 All right guys, so we're gonna do another leco challenge. It's called increasing order tree We're given a binary search tree and we want to rearrange the tree in an order So that the leftmost node in the tree is now the root of the tree and every node has no left child and only one Right child so as you can see here one two three one two three four five six Seven eight nine what we want to do is make sure that the tree is now in order and everything has is only There's no left child and everything is increasing order from the root from the smallest to largest Only through the right child. So I'll give you time to do this and then I'll explain my solution So what I did here in my solution is I kept a vector called it an integer that keeps track of all the values That we're going through and I'm going to use an in-order traversal So in order to use an in-order traversal I can create a function called in order in order is going to basically Go to the if the root is null then we return nothing It's going to go to the left node then go to the current node and then go to the right node So once we go to left node We pass in then left left node and the values that we're at Then it's going to go to the current node and we're going to add the current node to The current nodes value to our vector and it's going to go to the right node And then it does this recursively for in order traversal after in order is done We're going to essentially have a vector that has all the values in order The vector is an array that has all the values in order So it's going to go have one two three four five six seven eight nine Now what we're going to do is we're going to go through we're going to create a new tree node that This is going to be a dummy tree dummy tree node this dummy tree node is going to have Initial value of zero. So that's the initial values. This is a dummy tree node. That's what it's going to have We're going to have we're going to have a current pointer point to this new dummy tree node that has initial value zero now we're going to go through all the values of of the vector Vals because vows now has all the values in order, right because we did an in order traversal We're going to go through all the in order values of vows And basically what we're going to do is we're just going to create a new node every time With the the corresponding value and then we're going to set that Sets currents right node to equal that new node. Okay then we're going to Have current point to the next node every time we add a new node to ensure that every time we're going to add a new node It's going to always be the right node. So it's going to be one is going to add a new node to then of two so it's going to point the two to here and Add a new node of two to the right side and then it's going to have current point to two and Then it's going to add another node three and then increase the current to point to the three And then it's going to do I would go on and so forth then we're going to return answer that Answer right the reason why we're going to return answer right is because answer was our dummy node that had initial value zero So we want to return the right Answers right, which is going to be the first value that we added. Okay, because in this case We're going to assume that there's the root is actually going to be zero because we added a dummy node and we added all the increasing values based to the right of the dummy node So that's why we're going to return answer right and yeah, that's basically how you would do this That was my solution to this problem, but there is a better solution. Okay, so there's one better solution Another way to do this is through relinking. So this is a better in terms of space. This is a way better better solution to do What we're basically going to do is we are going to do the same thing of in order traversal we're going to do in order if root node is equal to null we return and then we go to the left child and then rather than adding the the current node to a vector and For all the values in order what we're going to do is we're going to do rerouting So what we're going to do is we're going to set currents nodes left is going to go point to null So that's going to get rid of the left node and then we're going to point the currents right node point to a Currents right now is going to point to the current node It's to the new node that we're adding and then we set current to is equal to the new node new nodes right position and then we pass in in order right reason what this is going to do is going to change the routing of the actual tree So basically what we're doing is we're having a global variable called tree node current and then this tree node current is Going to essentially do all the rerouting by going through every single node in the tree, but Changing it's the original trees value. So what we do is we create a new dummy We still have a dummy tree node called dummy tree node answer. We're going to have current point to that Originally, we're going to have current point to the dummy tree node We're going to call in order root and then the current is basically going to do all the rerouting here and Then we're going to return Answers right, which is the first value past our dummy tree node reason why we're going to return answer right is because all the values are going to get be added after our dummy Answer answers that the dummy tree node has value zero All of the first value is going to be the right value of our dummy tree node for adding after answers That's where we return answers right and that's going to prevent us from having that dummy tree node. So, yeah, that's all That's the better solution is that to actually modify the original value inside the Algorithm in the original tree. Hope you guys enjoyed this video raycom subscribe. I'll check