why am i getting run error here? it is working fine on netbeans.
import java.util.*;
public class Generic_tree {
private class Node
{
int data;
ArrayList children;
Node(int data)
{
this.data=data;
this.children=new ArrayList<>();
}
}
private Node root=null;
private int size=0;
public Generic_tree() {
Scanner s=new Scanner(System.in);
this.root=takeinput(s,null,0);
}
private Node takeinput(Scanner s,Node parent,int ith_child)
{
int nodedata=s.nextInt();
Node node=new Node(nodedata);
this.size++;
int children=s.nextInt();
for(int i=0;i<children;i++){
Node child=this.takeinput(s, node, i);
node.children.add(child);
}
return node;
}
public void display()
{
this.display(this.root);
}
private void display(Node node)
{
String str=node.data+"=>";
for(int i=0;i<node.children.size();i++)
{
str= str+ node.children.get(i).data+",";
}
str=str+"end";
System.out.println(str);
for(int i=0;i<node.children.size();i++)
{
this.display(node.children.get(i));
}
}
public int kth_sum(int k)
{
return this.kth_sum(this.root,k);
}
private int kth_sum(Node node,int k)
{
if(k<0)
return -1;
if(k==0)
return node.data;
int sum=0;
for(int i=0;i<node.children.size();i++)
{
sum=sum+kth_sum(node.children.get(i), k-1);
}
return sum;
}
// 1 2 2 2 3 0 4 0 5 2 6 0 7 0 2
public static void main(String[] args) {
Generic_tree tree=new Generic_tree();
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
System.out.println(tree.kth_sum(k));
}
}
this is the code .