Kindly tell me how to take the input tree

Kindly tell me how to take the input tree , in an ide as i am feeling a lot of issues while trying to solve the trees challanges .

Hello @Ramitgoel

Trees input are given in different formats in problems.
It can be
1.level order input format
2.pre/in/post order input format
3.pre/post order and in order combined

The problem you have attached, has level order input format
in Level order input format, the elements are given in the order as
Traverse each level (starting from 0) from left to right and print the element in each node (if it exists) or print -1 if the element does NOT exist.
so the input like 1 2 -1 3 4 -1 -1 -1 -1 would be a tree like

        1
      /
    2
   /  \
 3     4

So here we use a queue to take the input
first we will push the first element (if it NOT -1)
then for each element in the queue we have to input its left child and the right child.
so pop it out and take as input the left child and the right child (if they are NOT -1)
and also push them to the queue to take as input their child.
and the while loop will go on until there are no more elements in the queue to take inputs for.

The code is attached

1 Like

@prashuk156
ok thanks for the sol .