using System.Collections.Generic;
public List<Node> Children = new List<Node>();
public static void Main()
var root = new Node { Name = "root" };
Console.WriteLine("Creating tree");
Console.WriteLine("Calculating descendent count");
Console.WriteLine(CalculateDescendents(root));
private static void CreateTree(Node node, int level = 1, int maxDepth = 10)
var random = new Random();
var childrenCount = random.Next(1, 10);
for (var i = 0; i < childrenCount; i++)
var child = new Node { Name = string.Format("Level {0}, Child {1}", level, i) };
node.Children.Add(child);
CreateTree(child, level + 1);
private static int CalculateDescendents(Node node)
if (node.Children.Count == 0)
foreach (var child in node.Children)
totalChildCount += 1 + CalculateDescendents(child);