# Construct BST from its given level order traversal

Construct the BST (Binary Search Tree) from its given level order traversal.

Examples:

```Input : arr[] = {7, 4, 12, 3, 6, 8, 1, 5, 10}
Output : BST:
7
/ \
4   12
/ \  /
3  6 8
/  /   \
1   5   10
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

The idea is to use the Recursion:-
We know that the first element will always be the root of tree and second element will be the left child and third element will be the right child (if fall in the range), and so on for all the remaining elements.

1) First pick the first element of the array and make it root.
2) Pick the second element, if it’s value is smaller than root node value make it left child,
3) Else make it right child
4) Now recursively call the step (2) and step (3) to make a BST from its level Order Traversal.

Below is the implementation of above approach:

## C++

 `// C++ implementation to construct a BST ` `// from its level order traversal ` `#include ` ` `  `using` `namespace` `std; ` ` `  ` `  `// node of a BST ` `struct` `Node ` `{ ` `    ``int` `data; ` `    ``Node *left, *right; ` `}; ` ` `  ` `  `// function to get a new node ` `Node* getNode(``int` `data) ` `{ ` `    ``// Allocate memory ` `    ``Node *newNode = ` `        ``(Node*)``malloc``(``sizeof``(Node)); ` `     `  `    ``// put in the data     ` `    ``newNode->data = data; ` `    ``newNode->left = newNode->right = NULL;     ` `    ``return` `newNode; ` `} ` ` `  ` `  `// function to construct a BST from ` `// its level order traversal ` `Node *LevelOrder(Node *root , ``int` `data)  ` `{ ` `     ``if``(root==NULL){     ` `        ``root = getNode(data); ` `        ``return` `root; ` `     ``} ` `     ``if``(data <= root->data) ` `     ``root->left = LevelOrder(root->left, data); ` `     ``else` `     ``root->right = LevelOrder(root->right, data); ` `     ``return` `root;      ` `} ` ` `  `Node* constructBst(``int` `arr[], ``int` `n) ` `{ ` `    ``if``(n==0)``return` `NULL; ` `    ``Node *root =NULL; ` ` `  `    ``for``(``int` `i=0;ileft); ` `    ``cout << root->data << ``" "``; ` `    ``inorderTraversal(root->right);     ` `} ` ` `  ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``int` `arr[] = {7, 4, 12, 3, 6, 8, 1, 5, 10}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `     `  `    ``Node *root = constructBst(arr, n); ` `     `  `    ``cout << ``"Inorder Traversal: "``; ` `    ``inorderTraversal(root); ` `    ``return` `0;     ` `}  `

## Java

 `// Java implementation to construct a BST ` `// from its level order traversal ` `class` `GFG ` `{ ` ` `  `// node of a BST ` `static` `class` `Node ` `{ ` `    ``int` `data; ` `    ``Node left, right; ` `}; ` ` `  ` `  `// function to get a new node ` `static` `Node getNode(``int` `data) ` `{ ` `    ``// Allocate memory ` `    ``Node newNode = ``new` `Node(); ` `     `  `    ``// put in the data  ` `    ``newNode.data = data; ` `    ``newNode.left = newNode.right = ``null``;  ` `    ``return` `newNode; ` `} ` ` `  ` `  `// function to construct a BST from ` `// its level order traversal ` `static` `Node LevelOrder(Node root , ``int` `data)  ` `{ ` `    ``if``(root == ``null``) ` `    ``{  ` `        ``root = getNode(data); ` `        ``return` `root; ` `    ``} ` `    ``if``(data <= root.data) ` `    ``root.left = LevelOrder(root.left, data); ` `    ``else` `    ``root.right = LevelOrder(root.right, data); ` `    ``return` `root;      ` `} ` ` `  `static` `Node constructBst(``int` `arr[], ``int` `n) ` `{ ` `    ``if``(n == ``0``)``return` `null``; ` `    ``Node root = ``null``; ` ` `  `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``root = LevelOrder(root , arr[i]); ` `     `  `    ``return` `root; ` `} ` ` `  `// function to print the inorder traversal ` `static` `void` `inorderTraversal(Node root) ` `{ ` `    ``if` `(root == ``null``) ` `        ``return``; ` `     `  `    ``inorderTraversal(root.left); ` `    ``System.out.print( root.data + ``" "``); ` `    ``inorderTraversal(root.right);  ` `} ` ` `  ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = {``7``, ``4``, ``12``, ``3``, ``6``, ``8``, ``1``, ``5``, ``10``}; ` `    ``int` `n = arr.length; ` `     `  `    ``Node root = constructBst(arr, n); ` `     `  `    ``System.out.print( ``"Inorder Traversal: "``); ` `    ``inorderTraversal(root); ` `} ` `}  ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# Python implementation to construct a BST ` `# from its level order traversal ` `import` `math ` ` `  `# node of a BST ` `class` `Node:  ` `    ``def` `__init__(``self``,data):  ` `        ``self``.data ``=` `data  ` `        ``self``.left ``=` `None` `        ``self``.right ``=` `None` ` `  `# function to get a new node ` `def` `getNode( data): ` `     `  `    ``# Allocate memory ` `    ``newNode ``=` `Node(data) ` `     `  `    ``# put in the data  ` `    ``newNode.data ``=` `data ` `    ``newNode.left ``=``None` `    ``newNode.right ``=` `None` `    ``return` `newNode ` ` `  `# function to construct a BST from ` `# its level order traversal ` `def` `LevelOrder(root , data): ` `    ``if``(root ``=``=` `None``): ` `        ``root ``=` `getNode(data) ` `        ``return` `root ` `     `  `    ``if``(data <``=` `root.data): ` `        ``root.left ``=` `LevelOrder(root.left, data) ` `    ``else``: ` `        ``root.right ``=` `LevelOrder(root.right, data) ` `    ``return` `root      ` ` `  `def` `constructBst(arr, n): ` `    ``if``(n ``=``=` `0``): ` `        ``return` `None` `    ``root ``=` `None` ` `  `    ``for` `i ``in` `range``(``0``, n): ` `        ``root ``=` `LevelOrder(root , arr[i]) ` `     `  `    ``return` `root ` ` `  `# function to print the inorder traversal ` `def` `inorderTraversal( root): ` `    ``if` `(root ``=``=` `None``): ` `        ``return` `None` `     `  `    ``inorderTraversal(root.left) ` `    ``print``(root.data,end ``=` `" "``) ` `    ``inorderTraversal(root.right)  ` ` `  `# Driver program ` `if` `__name__``=``=``'__main__'``:  ` ` `  `    ``arr ``=` `[``7``, ``4``, ``12``, ``3``, ``6``, ``8``, ``1``, ``5``, ``10``] ` `    ``n ``=` `len``(arr) ` `     `  `    ``root ``=` `constructBst(arr, n) ` `     `  `    ``print``(``"Inorder Traversal: "``, end ``=` `"") ` `    ``root ``=` `inorderTraversal(root) ` `         `  `# This code is contributed by Srathore `

## C#

 `// C# implementation to construct a BST ` `// from its level order traversal ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// node of a BST ` `public` `class` `Node ` `{ ` `    ``public` `int` `data; ` `    ``public` `Node left, right; ` `}; ` ` `  `// function to get a new node ` `static` `Node getNode(``int` `data) ` `{ ` `    ``// Allocate memory ` `    ``Node newNode = ``new` `Node(); ` `     `  `    ``// put in the data  ` `    ``newNode.data = data; ` `    ``newNode.left = newNode.right = ``null``;  ` `    ``return` `newNode; ` `} ` ` `  `// function to construct a BST from ` `// its level order traversal ` `static` `Node LevelOrder(Node root,  ` `                       ``int` `data)  ` `{ ` `    ``if``(root == ``null``) ` `    ``{  ` `        ``root = getNode(data); ` `        ``return` `root; ` `    ``} ` `     `  `    ``if``(data <= root.data) ` `        ``root.left = LevelOrder(root.left, data); ` `    ``else` `        ``root.right = LevelOrder(root.right, data); ` `    ``return` `root;      ` `} ` ` `  `static` `Node constructBst(``int` `[]arr, ``int` `n) ` `{ ` `    ``if``(n == 0) ``return` `null``; ` `    ``Node root = ``null``; ` ` `  `    ``for``(``int` `i = 0; i < n; i++) ` `    ``root = LevelOrder(root, arr[i]); ` `     `  `    ``return` `root; ` `} ` ` `  `// function to print the inorder traversal ` `static` `void` `inorderTraversal(Node root) ` `{ ` `    ``if` `(root == ``null``) ` `        ``return``; ` `     `  `    ``inorderTraversal(root.left); ` `    ``Console.Write( root.data + ``" "``); ` `    ``inorderTraversal(root.right);  ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `[]arr = {7, 4, 12, 3,  ` `                 ``6, 8, 1, 5, 10}; ` `    ``int` `n = arr.Length; ` `     `  `    ``Node root = constructBst(arr, n); ` `     `  `    ``Console.Write(``"Inorder Traversal: "``); ` `    ``inorderTraversal(root); ` `} ` `}  ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Inorder Traversal: 1 3 4 5 6 7 8 10 12
```

Time Complexity : O(n2)
This is because the above program is like we are inserting n nodes in a bst which takes O(n2) time in the worst case.

This article is contributed by Nishant Balayan. 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.