Who is our boss
public static Employee closestCommonManagerRecur(Employee ceo, Employee firstEmployee, Employee secondEmployee)
{
if (ceo == null || firstEmployee == null || secondEmployee == null)
{
return null;
}
if (ceo.getId() == firstEmployee.getId())
{
return ceo;
}
if (ceo.getId() == secondEmployee.getId())
{
return ceo;
}
HashSet<Employee> em = new HashSet<Employee>();
foreach (var e in ceo.getReports())
{
var m = closestCommonManagerRecur(e, firstEmployee, secondEmployee);
if (m != null)
{
em.Add(m);
}
}
if (em.Count == 0)
{
return null;
}
if (em.Count == 2)
{
return ceo;
}
else
{
return em.ElementAt(0);
}
}
- Anonymous November 02, 2015