Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Create Binary Tree

Easy
0/40
profile
Contributed by
66 upvotes

Problem statement

Given an array 'arr' that contains 7 integers representing the values of nodes in a binary tree. This represents level order. The first element of the array represents the value of the root node.


Your objective is to construct a binary tree using the remaining 6 elements of the array, creating nodes for each of these values and return root node.


For example:
arr = [11, 22, 3, 54, 15, 23, 21]
The 7 node binary tree is represented below.

add-image

Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of  contains 7 integers of array ‘arr’.
Output Format:
The output contains the root element of the constructed tree. If the constructed tree is not formed correctly then output will be ‘-1’.
Note:-
You don’t need to print anything. Just implement the given function.
Sample Input 1:
1 2 3 4 5 6 7
Sample Output 1:
1
Explanation Of Sample Input 1:
The 7 node binary tree is represented below.

add-image

Sample Input 2:
11 22 3 54 15 23 21
Sample Output 2:
11
Constraints:
arr.length = 7
1<=arr[i]<=100
Time Limit: 1 sec
Full screen
Console