Update appNew update is available. Click here to update.

Construct complete Binary Tree

Last Updated: 26 Nov, 2020
Difficulty: Easy


Try Problem

You are given an array/list 'ARR' storing values of 'N' nodes of a binary tree.

Your task is to construct a complete binary tree from the given array in level order fashion, i.e. elements from left in the array will be filled in the tree level-wise starting from level 0.

A Binary Tree is a Complete Binary Tree if all the levels are completely filled except possibly the last level and the last level has all nodes as left as possible.

For example, the following binary tree is a complete binary tree:


You need to return the root of the binary tree, the tree will be printed in level order format.
Input Format:
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.

The first line of each test case contains an integer ‘N’ representing the size of the input array.

The second line of each test case contains elements of an array from which the complete binary tree is to be generated.

For example, the input array for the complete binary tree depicted in the below image would be [5, 6, 10, 2, 3, 5, 9]


Output Format:
For each test case, print a single line containing nodes of the binary tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place. Refer to the example given below.

The output for each test case is in a separate line.

For example, the output for the tree depicted in the below image would be:


6 10
2 3 5 9
-1 -1 -1 -1 -1 -1 -1 -1


Level 1:
The root node of the tree is 4

Level 2:
Left child of 5 = 6
Right child of 5 = 10

Level 3:
Left child of 6 = 2
Right child of 6 = 3
Left child of 10 = 5
Right child of 10 = 9

Level 4:
Left child of 2 = null (-1)
Right child of 2 = null (-1)
Left child of 3 = null (-1)
Right child of 3 = null (-1)
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 9 = null (-1)
Right child of 9 = null (-1)

The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The output ends when all nodes at the last level are null(-1).
The above format was just to provide clarity on how the output is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the output will be given as:

5 6 10 2 3 5 9 -1 -1 -1 -1 -1 -1 -1 -1
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 3000
0 <= ARR[i] <= 10 ^ 6

Time Limit: 1 sec

Approach 1

If we observe carefully we can see that if parent node is at index i in the array then the left child of that node is at index (2 * i + 1) and right child is at index (2 * i + 2) in the array.


Using this concept, we can easily insert the left and right nodes by choosing its parent node. We will insert the first element present in the array as the root node at level 0 in the tree and start traversing the array and for every node we will insert its both children left and right in the tree.

Try Problem