fork(7) 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. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println(countTrees(3, 1, 3));
  13. }
  14.  
  15. public static int countTrees(int N, int curH, int H) {
  16. if ( curH > H ) return 0;
  17.  
  18. if (N <=1) {
  19. return(1);
  20. }
  21. else {
  22. int sum = 0;
  23. int left, right, root;
  24.  
  25. for (root=1; root<=N; root++) {
  26. left = countTrees(root - 1, curH+1, H);
  27. right = countTrees(N - root, curH+1, H);
  28.  
  29. // number of possible trees with this root == left*right
  30. sum += left*right;
  31. }
  32.  
  33. return(sum);
  34. }
  35. }
  36. }
Success #stdin #stdout 0.12s 320576KB
stdin
Standard input is empty
stdout
5