Leetcode binary tree form preorder and inorder

there is slight error in my code plz tell me where I am wrong
plz make a change in my code only and comment while doing corrections
problem


my solution

Hello @coderajay03 what error you are facing?

bhaiya you can run it and will see there is little incorrectness in output you can see it on leetcode run only!!

bhaiya can u please tell me where I omitted mistake ??

Hello @coderajay03 please see the mistake here it is the corrected code:


if you have any doubt you can ask here:
Happy Learning!!

so when u made recursive call for left and right subtree you passed the same i value ?
WHY?
The i should be i+1 for left and i+2 for right subtree bcz that is how preorder i will move from i=0 then i+1 for getting LEFT TREE NODE and i+2 for RIGHT TREE node but you used same ‘i’ value why?

@coderajay03 see when we are finding preorder element in indoredr after that loop i have incremented i.
and in doing like your way it is giving error.
so that is why i have done like that.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.