Tree right view creating tree from level order

we need to first create a binary tree from level order to do this problem.Please explain how to do so with the code.

the idea is simple…you have to use a queue…and take input in level wise…first explicitly create a root and enqueue it in the queue…now take input till the time queue is not empty

first try to code yourself if you face any issue revert back

please provide code for same as i am not able to understand

refer to this code

package crux;
​
import java.util.*;
​
public class Tree {
static Scanner s = new Scanner(System.in);
​
public static Node takeInput() {
System.out.println(“Enter the root data”);
int rootData = s.nextInt();
if (rootData == -1) {
return null;
}
​
Node rootNode = new Node(rootData);
​
Queue pn = new LinkedList<>();//pn=pending nodes… queue used in level order
pn.add(rootNode);
​
while (!pn.isEmpty()) {
Node fn = pn.remove();//fn=front node
System.out.println("Enter the left child of " + fn.data);
int lc = s.nextInt();//left child of that node
if (lc != -1) {
Node leftChild = new Node(lc);
fn.left = leftChild;
pn.add(leftChild);
}
System.out.println("Enter the right child of " + fn.data);
int rc = s.nextInt();
if (rc != -1) {
Node rightChild = new Node(rc);
fn.right = rightChild;
pn.add(rightChild);
}
​
}
return rootNode;
}

public static void preO(Node root) {
	if(root==null) {
		return;
	}
	System.out.print(root.data+" ");
	preO(root.left);
	preO(root.right);
	
}

public static void main(String[] args) {
	Node root=takeInput();
	preO(root);
}

​
}
​
class Node {
int data;
Node left;
Node right;
​
public Node(int data) {
this.data = data;
}
}

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.