Could please suggest a way to input no of nodes in the tree and than print inorder traversal


i have tried this one but i am getting error

@Vipul.kapoor hey you can use level order input,In Level Order, you insert the nodes/traverse the tree level wise.
i.e.
first node at level 0(Root)
then you start filling the next level i.e. level 1 from left to right
Similar order is followed for other nodes.

Let’s understand with the help of example given in the question:
1 2 3 4 5 -1 6 -1 -1 -1 -1 -1 -1
-1 indicates no child.
The structure of the tree formed for the above example is:
_________________1
_________2______________3
_____4_______5_______________6
Refer this code:

The first line contains n , number of nodes in the tree.

Each of the next n lines contains two integers, a b , where a is the index of left child, and b is the index of right child of ith node.

in question i have been given to input no of elements or nodes in the tree. above mentioned is part of the queston. i also did level order traversal

@Vipul.kapoor hey please share link of question i will check and send you code for that.

i am having problem to input a tree in given format

@Vipul.kapoor hey you have to just take input in array and then they have given indexes of child so just run a loop and make elemengt at left index be left child of node and same for right,check this code:
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
tree = new node[n + 1];
depth = new ArrayList[n + 1];
for(int i = 1; i <= n; i++) {
tree[i] = new node(i);
depth[i] = new ArrayList<>();
}

    for(int i = 1; i <= n; i++) {
        int l = sc.nextInt();
        int r = sc.nextInt();
        if(l != -1) {
            tree[i].left = tree[l];
        }
        if(r != -1) {
            tree[i].right = tree[r];
        }
    }

could you please translate this in c or c++

@Vipul.kapoor hey in place of scanner just use cin ,rest same hai and see logic only ,which you want to know

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.