Pergunta de entrevista da empresa Amazon

Not too difficult. Some tree and array problems.