Amazon interview question

Not too difficult. Some tree and array problems.