jmincoder
BAN USERLet S(n) represent all possible trees of size n. Also all possible combinations of tree having left from S(x) and right from S(y) be represented as S(x)S(y)
Then S(n) can be calculated recursively from S(0) to S(n-1) by following
S(n) = S(0)S(n-1) + S(1)S(n-2) + ... + S(n-1)S(0)
S(0) would be an array of just one element representing null tree.
function all(n)
{
if (n == 0) { return [null]; }
var res = [];
for (i=0;i<n;i++)
{
foreach left in all(i)
{
foreach right in all(n-1-i)
{
res.add(new tree(left,right))
}
}
}
return res;
}
Rephenryhokinsh, Android test engineer at ABC TECH SUPPORT
I’m Henry, I am Children's librarian in Rolling Thunder .My Work involves the responsibility for supervising the children ...
Various degree rotations : really nice.
- jmincoder November 01, 2012