title: Computing the height of a tree - Java
body: <p>I currently have a method which computes the height of a tree but I need to speed up the algorithm so that it may run under 6 seconds. I was thinking of implementing something recursive but unsure of how to do so.</p>
  
  <pre><code>public class TreeHeight {
      int n;
      int parent[];
  
      void read() throws IOException {
          FastScanner in = new FastScanner();
          n = in.nextInt();
          parent = new int[n];
          for (int i = 0; i &lt; n; i++) {
              parent[i] = in.nextInt();
          }
      }
  
      int computeHeight() {
                      // Replace this code with a faster implementation
          int maxHeight = 0;
          for (int vertex = 0; vertex &lt; n; vertex++) {
              int height = 0;
              for (int i = vertex; i != -1; i = parent[i])
                  height++;
              maxHeight = Math.max(maxHeight, height);// assign the max of the two arguements
          }
          return maxHeight;
      }
  }
  </code></pre>