In this question first I need to construct the tree then only I can solve further???
Doubt in the question
Sir,I am not able to construct the tree ,can you please give me the code or idea how to construct tree if this type of array is given ,I am stuck when I am getting -1 in my array???
to create a binary tree from level order input, we can build this binary tree by maintaining a queue. Queue is used to maintain those nodes that are not yet processed.
Using a variable count to keep track of the number of children added for the current node.
First, create a root node, assign it as the current node. So starting from index 1 (index 0 is the root), as the count is 0, we add this node as left child of the current node. Increase count. If this node is not ‘-1’, add it to the queue.
Moving to the next index, the count is 1, so we add this as right child of current node, reset count to 0 and update current node (by assigning the current node as the first element in the queue). If this node is not ‘-1’, add it to the queue.
can you please give the code??
@Adhyayan,
https://ide.codingblocks.com/s/224026 This is a sample approach. Modify it according to your code. You can attempt the question and reply to me on this thread if you face any problem.
Sir please comment the errors …
We are not given the size of the array input. We are only given the elements. You can try to use a arraylist. Your logic should work. Sort out the input though.
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.
Sir how to take the input in array list for this case ??how to know the terminating point of input ?? When to stop taking any further value in our input??
again some error is coming
@Adhyayan,
https://ide.codingblocks.com/s/228177 corrected code.
Please see the input format.
Also in the rightview method keep the maxl variable global instead of passing it for every iteration.