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

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 code to perform the above mentioned data entry.

## C++

`// CPP program to construct a binary tree in level order. ` `#include <bits/stdc++.h> ` `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<Node *>& 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<Node*> 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<Node*> 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; ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 program to construct ` `# a binary tree in level order. ` ` ` `# Importing Queue for use in ` `# Level Order Traversal ` `import` `queue ` ` ` `# Node class for holding the Binary Tree ` `class` `node: ` ` ` `def` `__init__(` `self` `, data ` `=` `None` `): ` ` ` `self` `.data ` `=` `data ` ` ` `self` `.left ` `=` `None` ` ` `self` `.right ` `=` `None` ` ` `Q ` `=` `queue.Queue() ` ` ` `# Helper function helps us in adding data ` `# to the tree in Level Order ` `def` `insertValue(data, root): ` ` ` `newnode ` `=` `node(data) ` ` ` `if` `Q.empty() !` `=` `True` `: ` ` ` `temp ` `=` `Q.get() ` ` ` `if` `root ` `=` `=` `None` `: ` ` ` `root ` `=` `newnode ` ` ` ` ` `# The left child of the current Node is ` ` ` `# used if it is available. ` ` ` `elif` `temp.left ` `=` `=` `None` `: ` ` ` `temp.left ` `=` `newnode ` ` ` ` ` `# 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. ` ` ` `elif` `temp.right ` `=` `=` `None` `: ` ` ` `temp.right ` `=` `newnode ` ` ` `atemp ` `=` `Q.get() ` ` ` ` ` `# 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.put(newnode) ` ` ` `return` `root ` ` ` `# Function which calls add which is responsible ` `# for adding elements one by one ` `def` `createTree(a, root): ` ` ` `for` `i ` `in` `range` `(` `len` `(a)): ` ` ` `root ` `=` `insertValue(a[i], root) ` ` ` `return` `root ` ` ` `# Function for printing level order traversal ` `def` `levelOrder(root): ` ` ` `Q ` `=` `queue.Queue() ` ` ` `Q.put(root) ` ` ` `while` `Q.empty() !` `=` `True` `: ` ` ` `temp ` `=` `Q.get() ` ` ` `print` `(temp.data, end ` `=` `' '` `) ` ` ` `if` `temp.left !` `=` `None` `: ` ` ` `Q.put(temp.left) ` ` ` `if` `temp.right !` `=` `None` `: ` ` ` `Q.put(temp.right) ` ` ` `# Driver Code ` `a ` `=` `[ ` `10` `, ` `20` `, ` `30` `, ` `40` `, ` `50` `, ` `60` `] ` `root ` `=` `None` `root ` `=` `createTree(a, root) ` ` ` `levelOrder(root) ` ` ` `# This code is contributed by code_freak ` |

*chevron_right*

*filter_none*

**Output:**

10 20 30 40 50 60

Time Complexity : O(n)

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.

## Recommended Posts:

- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Flatten Binary Tree in order of Level Order Traversal
- Insertion in n-ary tree in given order and Level order traversal
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Connect Nodes at same Level (Level Order Traversal)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Difference between sums of odd level and even level nodes of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Level Order Tree Traversal
- Construct a tree from Inorder and Level order traversals | Set 1
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Print extreme nodes of each level of Binary Tree in alternate order
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Construct a complete binary tree from given array in level order fashion
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Zig Zag Level order traversal of a tree using single array

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.