rt,
三层四层二叉树有多少种,延伸下就是问题:二叉树不同层数的有多少种?
0层的二叉树有1种;a0
C#
codes as below:
using System;
namespace CountTreeNumber
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(CountTreeHelper.CountTree(i));
}
Console.ReadKey();
}
}
class CountTreeHelper
{
public static double CountTree(double depth)
{
if (depth == 1)
return 1;
double count1 = CountTree(depth - 1);
double countPart1 = 2 * count1;
double count2=0;
for (double i = 1; i < depth; i++)
{
count2 += CountTree(i);
}
double countPart2 = count1 * (count2) * 2;
double duplcatedCountPart2 = count1 * count1;
return countPart1 + countPart2 - duplcatedCountPart2;
}
}
}