CSharp · Console
// Write a function that takes the tree represented as “sons” array and
// returns the length (=number of edges) of the longest possible path
// from the root to a leaf (this number is called the height of the tree).
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
int[][] sons = new int[5][];
sons[0] = new int[] { 1, 2 };
sons[1] = new int[] { };
sons[2] = new int[] { 3 };
sons[3] = new int[] { 4 };
sons[4] = new int[] { };
Console.WriteLine("Depth={0}", Depth(sons, 0));
}
public static int Depth(int [][] sons, int node)
{
int depth = 0;
foreach(int child in sons[node])
{
depth = Math.Max(depth, 1 + Depth(sons, child));
}
return depth;
}
}