public static bool IsBalanced(int [][] sons)
while(sons[node].Length > 0)
return DepthIs(sons, 0, depth);
public static bool DepthIs(int [][] sons, int node, int depth)
if(depth == 0 || sons[node].Length == 0)
return depth == 0 && sons[node].Length == 0;
foreach(int child in sons[node])
if(!DepthIs(sons, child, depth - 1))
public static void Main()
int [] LEAF = new int[0];
int [][] justRoot = new int[][] { LEAF };
int [][] fork = new int[][] { new int[]{ 1, 2}, LEAF, LEAF };
int [][] fork4 = new int[][] { new int[]{ 1, 2}, new int [] {3}, LEAF, LEAF };
int [][] singleLeaf = new int[][] { new int[]{ 1}, new int [] {2}, LEAF };
Test(singleLeaf, "singleLeaf");
public static void Test(int [][] sons, string name)
Console.WriteLine("{0} is {1}", name, IsBalanced(sons) ? "balanced" : "unbalanced");