fork(3) 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){ this.level = level;}
  13. }
  14.  
  15. public static void main (String[] args) throws java.lang.Exception{
  16. String[] h = {"H1", "H1", "H2", "H3", "H3", "H5", "H2", "H2", "H3",
  17. "H4", "H2", "H2", "H2", "H1", "H2", "H2", "H3",
  18. "H4", "H4", "H2"};
  19.  
  20. Node[] mostRecent = new Node[6];
  21. mostRecent[0] = new Node(0); // root node
  22.  
  23. for(int i = 0; i < h.length; i++){
  24. int level = Integer.parseInt("" + h[i].charAt(1));
  25. Node n = new Node(level);
  26. mostRecent[level] = n;
  27.  
  28. int pLevel = level - 1;
  29. while(mostRecent[pLevel] == null) --pLevel;
  30. mostRecent[pLevel].children.add(n);
  31.  
  32. for(int j = 0; j < pLevel; j++)
  33. System.out.print("\t");
  34. System.out.println(h[i]);
  35. }
  36. }
  37. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
H1
H1
	H2
		H3
		H3
			H5
	H2
	H2
		H3
			H4
	H2
	H2
	H2
H1
	H2
	H2
		H3
			H4
			H4
	H2