Pregunta de entrevista de Amazon

Not too difficult. Some tree and array problems.