THE CODE BELOW IS GIVING CORRECT OUTPUTS IN NETBEANS. BUT IN IDE OF CB, IT IS SHOWING ERROR, THAT IS, “NO SUCH ELEMENT EXCEPTION” DURING COMPILATION. THE CODE IS RUNNING FOR THE FIRST TREE BUT SHOWING EXCEPTION FOR SECOND TREE. PLEASE CHECK WHAT IS THE PROBLEM.
import java.util.*;
public class Main
{
private class Node
{
int data;
Node left;
Node right;
Node(int data,Node left,Node right)
{
this.data=data;
this.left=left;
this.right=right;
}
}
Node root=null;
int size=0;
Main()
{
Scanner s=new Scanner(System.in);
this.root=takeInput(s,null,false); //
}
public Node takeInput(Scanner s,Node parent,boolean isLeftOrRight)
{
int nodeData=s.nextInt(); //
Node node=new Node(nodeData,null,null);
this.size++;
boolean choice=false;
choice=s.nextBoolean();
if(choice)
node.left=takeInput(s,node,true);
choice=false;
choice=s.nextBoolean();
if(choice)
node.right=takeInput(s,node,false);
return node;
}
public ArrayList<Boolean> display(Node node,ArrayList<Boolean> list)
{
String str="";
if(node.left!=null)
{
str=str+node.left.data+"=>";
list.add(true);
}
else
{
str=str+"END=>";
list.add(false);
}
str=str+node.data;
if(node.right!=null)
{
str=str+"<=" + node.right.data;
list.add(true);
}
else
{
str=str+"<=END";
list.add(false);
}
if(node.left!=null)
this.display(node.left,list);
if(node.right!=null)
this.display(node.right,list);
return list;
}
public boolean check(ArrayList<Boolean> list1,ArrayList<Boolean> list2)
{
for(int i=0;i<list1.size();i++)
{
if(list1.get(i)!=list2.get(i))
return false;
}
return true;
}
public static void main(String[] args)
{
Main tree1=new Main();
ArrayList<Boolean> ans1=tree1.display(tree1.root,new ArrayList<>());
Main tree2=new Main();
ArrayList<Boolean> ans2=tree2.display(tree2.root,new ArrayList<>());
if(ans1.size()!=ans2.size())
System.out.println(false);
else
System.out.println(tree1.check(ans1,ans2));
}
}

