hot to take input to create this tree
How to take tree as input
create nodes for all the values where value is not equal to -1 and it’s left child has index 2 * i and right child has index 2 * i + 1 in the array
can it be done using queue
Can be done but that would be an overkill
can u share the coode to build tree
I’m not allowed to write the code for you. I have given you the hint. That’s is pretty explanatory in itself. Try yourself
ok i will write and confirm with you
just take the entire string as input, split it using " " and it automatically stores the result in an array. Just take the size of that array
I don’t see any other easy way out to this. Infact, this is pretty easy if you give it a try.
if i made the array how will i make a tree of it and print right view
the code is implemented through queue only
here is how you can implement it using array
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] s1 = in.readLine().split(" ");
int[] A = new int[s1.length];
for(int i=0;i<s1.length;i++)
{
A[i] = Integer.parseInt(s1[i]);
}
Node[] B = new Node[A.length];
int n = A.length;
for(int i=0;i<n;i++)
{
B[i] = new Node();
if(A[i] != -1)
B[i].data = A[i];
}
for(int i=0;i<n;i++)
{
if(A[i] != -1)
{
B[i].left = B[2 * i + 1];
B[i].right = B[2 * i + 2];
}
}
thanks @saurabh_singhCmn i have not seen this kind of approach in this course so it was tough for me to write this
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.