# Construct BST from given preorder traversal | Set 3 (Naive Method)

Given preorder traversal of a binary search tree, construct the BST.

For example, if the given traversal is {10, 5, 1, 7, 40, 50}, then the output should be root of following tree.

```    10
/   \
5     40
/  \      \
1    7      50
```

We have discussed methods to construct binary search tree in previous posts.Here is another method to construct binary search tree when given preorder traversal.

We know that the inorder traversal of the BST gives the element in non-decreasing manner. Hence we can sort the given preorder traversal to obtain the inorder traversal of the binary search tree.

We have already learnt the method to construct tree when given preorder and inorder traversals in this post. We will now use the same method to construct the BST.

 `#include ` `using` `namespace` `std; ` ` `  `// A BST node has data, pointer to left  ` `// child and pointer to right child ` `struct` `Node { ` `    ``int` `data; ` `    ``Node *left, *right; ` `}; ` ` `  `// A utility function to create new node ` `Node* getNode(``int` `data) ` `{ ` `    ``Node* temp = ``new` `Node(); ` `    ``temp->data = data; ` `    ``temp->left = temp->right = NULL; ` ` `  `    ``return` `temp; ` `} ` ` `  `/* Recursive function to construct BST ` `Inorder traversal in[] and Preorder traversal ` `pre[]. Initial values of inStart and inEnd should be ` `0 and n -1.*/` `Node* buildBTRec(``int` `in[], ``int` `pre[], ``int` `inStart,  ` `            ``int` `inEnd, unordered_map<``int``, ``int``>& m) ` `{ ` `    ``static` `int` `preIdx = 0; ` `    ``if` `(inStart > inEnd) ` `        ``return` `NULL; ` ` `  `    ``// Pick current node from Preorder traversal ` `    ``// using preIndex and increment preIndex ` `    ``int` `curr = pre[preIdx]; ` `    ``++preIdx; ` ` `  `    ``Node* temp = getNode(curr); ` ` `  `    ``// If this node has no children then return ` `    ``if` `(inStart == inEnd) ` `        ``return` `temp; ` ` `  `    ``// Else find the index of this node in ` `    ``// inorder traversal ` `    ``int` `idx = m[curr]; ` ` `  `    ``// Using this index construct left and right subtrees ` `    ``temp->left = buildBTRec(in, pre, inStart, idx - 1, m); ` `    ``temp->right = buildBTRec(in, pre, idx + 1, inEnd, m); ` ` `  `    ``return` `temp; ` `} ` ` `  `// This function mainly creates a map to store  ` `// the indices of all items so we can quickly  ` `// access them later. ` `Node* buildBST(``int` `pre[], ``int` `n) ` `{ ` `    ``// Copy pre[] to in[] and sort it ` `    ``int` `in[n]; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``in[i] = pre[i]; ` `    ``sort(in, in + n); ` ` `  `    ``unordered_map<``int``, ``int``> m; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``m[in[i]] = i; ` `    ``return` `buildBTRec(in, pre, 0, n - 1, m); ` `} ` ` `  `// Utility function to do Inorder traversal of the tree ` `void` `inorderTraversal(Node* root) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` `    ``inorderTraversal(root->left); ` `    ``cout << root->data << ``" "``; ` `    ``inorderTraversal(root->right); ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``int` `pre[] = { 100, 20, 10, 30, 200, 150, 300 }; ` `    ``int` `n = ``sizeof``(pre) / ``sizeof``(pre); ` ` `  `    ``Node* root = buildBST(pre, n); ` ` `  `    ``// Let's test the built tree by printing its  ` `    ``// Inorder traversal ` `    ``cout << ``"Inorder traversal of the tree is \n"``; ` `    ``inorderTraversal(root); ` `    ``return` `0; ` `} `

Output:

```Inorder traversal of the tree is
10 20 30 100 150 200 300
```

Time Complexity: Sorting takes O(nlogn) time for sorting and constructing using preorder and inorder traversals takes linear time. Hence overall time complexity of the above solution is O(nlogn).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.