# Create a tree in level order

Given an array of elements, the task is to insert these elements in level order and construct a tree.

```Input : arr[] = {10, 20, 30, 40, 50, 60}
Output :         10
/      \
20       30
/  \     /
40    50  60
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The task is to construct whole tree from given array. To insert in level order in already constructed tree, please see Insertion in a Binary Tree in level order

The task is to store data in a binary tree, but in level order.

To do so, we will proceed as follows:
1. Whenever a new Node is added to the binary tree, the address of the node is pushed into a queue.
2. A Node address will stay in the queue until both its children Nodes do not get filled.
3. Once both the children Nodes get filled up, the parent Node is popped from the queue.

Here is the C++ code to perform the above mentioned data entry.

 `// CPP program to construct a binary tree in level order. ` `#include ` `using` `namespace` `std; ` ` `  `struct` `Node { ` `    ``int` `key; ` `    ``Node* left; ` `    ``Node* right; ` `}; ` ` `  `// Function to create a node with 'value' as the data ` `// stored in it. ` `// Both the children of this new Node are initially null. ` `struct` `Node* newNode(``int` `value) ` `{ ` `    ``Node* n = ``new` `Node; ` `    ``n->key = value; ` `    ``n->left = NULL; ` `    ``n->right = NULL; ` `    ``return` `n; ` `} ` ` `  `struct` `Node* insertValue(``struct` `Node* root, ``int` `value, ` `                         ``queue& q) ` `{ ` `    ``Node* node = newNode(value); ` `    ``if` `(root == NULL) ` `        ``root = node; ` ` `  `    ``// The left child of the current Node is ` `    ``// used if it is available. ` `    ``else` `if` `(q.front()->left == NULL) ` `        ``q.front()->left = node; ` ` `  `    ``// The right child of the current Node is used ` `    ``// if it is available. Since the left child of this ` `    ``// node has already been used, the Node is popped ` `    ``// from the queue after using its right child. ` `    ``else` `{ ` `        ``q.front()->right = node; ` `        ``q.pop(); ` `    ``} ` ` `  `    ``// Whenever a new Node is added to the tree, its ` `    ``// address is pushed into the queue. ` `    ``// So that its children Nodes can be used later. ` `    ``q.push(node); ` `    ``return` `root; ` `} ` ` `  `// This function mainly calls insertValue() for ` `// all array elements. All calls use same queue. ` `Node* createTree(``int` `arr[], ``int` `n) ` `{ ` `    ``Node* root = NULL; ` `    ``queue q; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `      ``root = insertValue(root, arr[i], q); ` `    ``return` `root; ` `} ` ` `  `// This is used to verify the logic. ` `void` `levelOrder(``struct` `Node* root) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` `    ``queue n; ` `    ``n.push(root); ` `    ``while` `(!n.empty()) { ` `        ``cout << n.front()->key << ``" "``; ` `        ``if` `(n.front()->left != NULL) ` `            ``n.push(n.front()->left); ` `        ``if` `(n.front()->right != NULL) ` `            ``n.push(n.front()->right); ` `        ``n.pop(); ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 10, 20, 30, 40, 50, 60 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``Node* root = createTree(arr, n); ` `    ``levelOrder(root); ` `    ``return` `0; ` `} `

Output:

```10 20 30 40 50 60
```

Time Complexity : O(n)

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.