I cannot understand, how I can create a tree in level order. I think I should make a queue and use it but still, I cannot understand the implementation
Constructing a level order tree
Hey @satwik7142
you can see this
public class Main {
static Scanner sc = new Scanner(System.in);
private class Node {
Node left;
Node right;
int data;
}
private Node root;
public Main() {
// TODO Auto-generated constructor stub
this.root = createNode();
}
private Node createNode() {
int d = sc.nextInt();
LinkedList q = new LinkedList();
Node nn = new Node();
nn.data = d;
this.root = nn;
q.add(root);
while (!q.isEmpty()) {
Node node = q.getFirst();
q.removeFirst();
int c1 = sc.nextInt();
int c2 = sc.nextInt();
if (c1 != -1) {
nn = new Node();
nn.data = c1;
node.left = nn;
q.addLast(node.left);
}
if (c2 != -1) {
nn = new Node();
nn.data = c2;
node.right = nn;
q.addLast(node.right);
}
}
return root;
}
}