Amazon student Interview Question
StudentsCountry: Germany
Interview Type: Phone Interview
bool IsBalanced(Node root)
{
int max = MaxDepth(curr);
int min = MinDepth(curr);
return max/min < 2;
}
int MaxDepth(Node curr)
{
if(curr == null) return 0;
return Math.Max(MaxDepth(curr.Left) + 1, MaxDepth(curr.Right) + 1);
}
int MinDepth(Node curr)
{
if(curr == null) return 0;
return Math.Min(MinDepth(curr.Left) + 1, MinDepth(curr.Right) + 1);
}
- mombasa February 17, 2014