fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone{
  9. public static class Node{
  10. int level;
  11. List<Node> children = new ArrayList<Node>();
  12. Node(int level){
  13. this.level = level;
  14. }
  15.  
  16. @Override public String toString(){
  17. StringBuilder sb = new StringBuilder();
  18. for(int i=1; i<level; i++)
  19. sb.append("\t");
  20. sb.append("H" + level + "\n");
  21. for(Node n : children)
  22. sb.append(n.toString());
  23.  
  24. return sb.toString();
  25. }
  26. }
  27.  
  28. public static void main (String[] args) throws java.lang.Exception{
  29. String[] h = {"H1", "H1", "H2", "H3", "H3", "H2", "H2", "H3",
  30. "H4", "H2", "H2", "H2", "H1", "H2", "H2", "H3",
  31. "H4", "H4", "H2"};
  32.  
  33. Node[] mostRecent = new Node[5];
  34. mostRecent[0] = new Node(0); // root node
  35.  
  36. for(int i = 0; i < h.length; i++){
  37. int level = Integer.parseInt("" + h[i].charAt(1));
  38. Node n = new Node(level);
  39. mostRecent[level - 1].children.add(n);
  40. mostRecent[level] = n;
  41. }
  42.  
  43. System.out.print(mostRecent[0]);
  44. }
  45. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
H0
H1
H1
	H2
		H3
		H3
	H2
	H2
		H3
			H4
	H2
	H2
	H2
H1
	H2
	H2
		H3
			H4
			H4
	H2