How to construct Binary Tree using the given Level Order Array

i am not able to construct the binary tree using Level order array

@jatin111 hi buddy, the code is very simple and self explanatory, just a queue will be used here.

public static Node buildTree(Scanner scn) {
int data = scn.nextInt();

	Node root = new Node(data, null, null);

	Queue<Node> queue = new LinkedList<Node>();
	queue.add(root);

	while(!queue.isEmpty()) {
		Node node = queue.peek();
		queue.remove();

		int data1 = scn.nextInt();
		int data2 = scn.nextInt();

		if(data1 != -1) {
			Node nodeL = new Node(data1, null, null);
			node.left = nodeL;
			queue.add(nodeL);
		}

		if(data2 != -1) {
			Node nodeR = new Node(data2, null, null);
			node.right = nodeR;
			queue.add(nodeR);
		}
	}
	return root;
}

first dry run it for sample, if you have any doubt then lemme know!
also if doubt is resolved, close it by marking it resolved.
Happy coding!

i have understood the your code but i dont know why it is working properly when i tried it.

link to the code https://ide.codingblocks.com/s/324505

please reply back to my query

@jatin111 Bro, the thing is start debugging things in your code.
Now i am sure you will be getting an exception but why?

   Main() {

        this.root = buildTree(sc);

    } 

Now see this -

bt.buildTree(sc);

Now you are calling the method two times on single input, so correct this mistake of yours!

Thank you!

thanks , i really cant believe that i did such a mistake.