rt,
三层四层二叉树有多少种,延伸下就是问题:二叉树不同层数的有多少种?
0层的二叉树有1种;a0 <wbr><wbr>= <wbr><wbr>1; <wbr><wbr><br><wbr><wbr>一层的二叉树有1种;a1 <wbr><wbr>= <wbr><wbr>1; <wbr><wbr><br><wbr><wbr>二层的二叉树有3种;a2 <wbr><wbr>= <wbr><wbr>2*(a0*a1)+a1*a1 <wbr><wbr>= <wbr><wbr>3; <wbr><wbr><br><wbr><wbr>三层的二叉树有2*(a0*a2+a1*a2)+a2*a2 <wbr><wbr>= <wbr><wbr>21种; <wbr><wbr><br><wbr><wbr>四层的二叉树有2*(a0*a3+a1*a3+a2*a3)+a3*a3 <wbr><wbr>= <wbr><wbr>651种;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
C#
codes as below:
usingSystem;
namespaceCountTreeNumber
{
classProgram
{
staticvoidMain(string[]args)
{
for(inti=1;i<=5;i++)
{
Console.WriteLine(CountTreeHelper.CountTree(i));
}
Console.ReadKey();
}
}
classCountTreeHelper
{
publicstaticdoubleCountTree(doubledepth)
{
if(depth==1)
return1;
doublecount1=CountTree(depth-1);
doublecountPart1=2*count1;
doublecount2=0;
for(doublei=1;i<depth;i++)
{
count2+=CountTree(i);
}
doublecountPart2=count1*(count2)*2;
doubleduplcatedCountPart2=count1*count1;
returncountPart1+countPart2-duplcatedCountPart2;
}
}
}