Not understand how tree will be made?

Can you explain how to buildtree from given string input?

The input given is level order. You have to build the tree accordingly.
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.

You can refer this sample code https://ide.codingblocks.com/s/60835

@pratyush63 ,These things I know but actually in this question input is given in the form of string which contains , [ ] and also null.I am asking for this case!

You will have to remove [ and ] and then tokenize the string with comma as deliminator.Use strtok function. Then you can put the value of each node in an array.
Refer this https://www.youtube.com/watch?v=i2ljAWLey00 from 57:11

1 Like

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.