Construct bst from pre order

plzz explain me the solution of this problem

Hello @ynikhil1999,

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

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

BST()

  1. Make the first element as root:
  2. find the first element in the sequence which is greater than root.
    the element between the index next to root and index just before first greater element will form the left subtree of the BST.
  3. Similarly, the element between the index of first greater element and end of sequence will form the right subtree of the BST.
  4. Make Recursive calls for 2. and 3.

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.