Construct complete Binary Tree
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:
Note:
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:
5
6 10
2 3 5 9
-1 -1 -1 -1 -1 -1 -1 -1
Explanation:
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).
Note:
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
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 3000
0 <= ARR[i] <= 10 ^ 6
Time Limit: 1 sec
CodingNinjas
author
2y
Step 1 : Using a map with regex
Step 2 : Creating an object initializing it to null
Step 3 : Taking for loop till the length
Step 4 : Checking for parent and child
Step 5 : Each child cannot have 2 parent...read more
CodingNinjas
author
2y
Recursive approach
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 ...read more
Help your peers!
Add answer anonymously...
Top Codalien Technologies Software Engineer interview questions & answers
Popular interview questions of Software Engineer
>
Codalien Technologies Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app