Input Approach : Size of input is not given

How will I take the input if size of the array is not mentioned (for the level order traversal )?
Do I need to make 10^7 (given constraint) nodes and assign others as null ?
Kindly suggest me how to implement this approach.

you could use BufferedReader for this purpose.

String[] A = bf.readLine().split(" ");
int n = A.length;
int[] B = new int[n];
for(int i=0;i<n;i++)
{
B[i] = Integer.parseInt(A[i]);
}

This way you get your input in B. Now start creating nodes and assign the nodes at indices B[2 * i + 1] and B[2 * i + 2] as its two children(check if it is not -1).

Please check the logic of my code. What I did was - I used the queue approach to store the nodes and respectively called their left and right branches. But I am still facing errors while submitting the code.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.