import java.io.*; import java.util.*; class C implements Serializable { public String id; public C l; public C p; public C(String id) { this.id = id; } public String s() { String s = id; if (l != null) s += " " + l.id; if (p != null) s += " " + p.id; return s; } public String r(List list) { String s = id; list.add(this); if (l != null) { s += " " + l.id; if (!list.contains(l)) s += " " + l.r(list); } if (p != null) { s += " " + p.id; if (!list.contains(l)) s += " " + p.r(list); } return s; } public static void main(String[] args) throws ClassNotFoundException, IOException { C a = new C("a"); C b = new C("b"); C c = new C("c"); C d = new C("d"); C e = new C("e"); a.l = c; a.p = d; b.l = d; c.l = b; e.l = b; ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("f.out")); out.writeObject(e); out.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream("f.out")); C x = (C) in.readObject(); in.close(); System.out.println(x.r(new ArrayList())); } }