|
|
back to boardCrash#1 Why am i getting Crash with this solution in Java? Thank you. public static Vector<Integer> res=new Vector<Integer>(); public static class Tree{ public Tree l; public Tree r; int val; Tree(int v){ this.val=v; this.l=null; this.r=null; } public static void add(Tree t,int v){ if(v<t.val){ if(t.l==null){ t.l=new Tree(v); } else{ add(t.l,v); } } else if(v>t.val){ if(t.r==null){ t.r=new Tree(v); } else{ add(t.r,v); } } } public static void rightSearch(Tree t){ if(t.r!=null) rightSearch(t.r); if(t.l!=null) rightSearch(t.l); res.add(t.val); } } public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); int n=Integer.parseInt(in.readLine()); String[] input=in.readLine().split(" "); Tree t=new Tree(Integer.parseInt(input[n-1])); for(int i=n-2; i>=0; i--){ Tree.add(t,Integer.parseInt(input[i])); } Tree.rightSearch(t); for(int i=0; i<res.size(); i++){ out.print(res.get(i)+" "); } out.close(); } |
|
|