Given number of binary tree nodes (X) write method that returns the number of random permutations of binary trees with X nodes.
Examples:
X=1: 1
o
X=2: 2
o o
o o
X=3: 5
o o o o o
o o o o o o
o o o o
I ended up with :
public static int numOfPerms(int numOfNodes) {
if (numOfNodes<=2 && numOfNodes > 0) {
return numOfNodes;
}
int res = 1;
for (int i=1; i<=numOfNodes; i++) {
res = res*(4*i-1)/(i+1);
}
return res;
}
I would appreciate sharing here better solutions.