here is the code. i've marked where i'm getting the error, the tutorial i'm following has the same code and it's working fine. when i checked the functions available in the queue interface in java collection frameworks, it's showing the return type of .remove() as object. am i using a different java version or what?
import java.util.*;
public class binaryTree{
static class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
static class BT{
static int idx = -1;
public static Node buildTree(int nodes[]){
idx++;
if(nodes[idx]==-1){
return null;
}
Node newNode = new Node(nodes[idx]);
newNode.left = buildTree(nodes);
newNode.right = buildTree(nodes);
return newNode;
}
}
public static void preOrder(Node root){
if(root==null) {System.out.println(-1); return;}
System.out.println(root.data);
preOrder(root.left);
preOrder(root.right);
}
public static void inOrder(Node root){
if(root==null) { System.out.println(-1); return;}
inOrder(root.left);
System.out.println(root.data);
inOrder(root.right);
}
public static void postOrder(Node root){
if(root==null) {return;}
postOrder(root.left);
postOrder(root.right);
System.out.println(root.data);
}
public static void levelOrder(Node root){
if(root==null) return;
Queue q = new LinkedList<>();
q.add(root);
q.add(null);
while(!q.isEmpty()){
Node currNode = q.remove(); //over here
if(currNode==null){
System.out.println();
if(q.isEmpty()){
break;
}else{
q.add(null);
}
}else{
System.out.println(currNode+" ");
if(currNode.left!=null)
q.add(currNode.left);
if(currNode.right!=null)
q.add(currNode.right);
}
}
}
public static void main(String[] args) {
int nodes[] = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1};
BT tree = new BT();
Node root = tree.buildTree(nodes);
// System.out.println(root.data);
// preOrder(root);
// inOrder(root);
// postOrder(root);
levelOrder(root);
}
}