Generating inorder from preorder

is there any way so that we can generate inorder by seing preoder…?

Hello @ynikhil1999,

Suppose, you have given a preorder traversal as: 10, 5, 1, 7, 40, 50
Now you have to find the preorder of:
__________________10
___________5_____________40
______1__________7____________50

You can use recursion for the same:
If you notice, the first element of the inorder is the root.

In_Pre()
Make the first element as root:

  1. find the first element in the sequence which is greater than root.
  2. the element between the index next to root and index just before first greater element will form the left subtree of the BST. Make a recursive call.
  3. Print value of current root.
  4. Similarly, the element between the index of first greater element and end of sequence will form the right subtree of the BST. Make a recursive call.

Base condition: when the input sequence is of length n==1, print root and return.

Hope, this would help.
Give a like, if you are satisfied.

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.