Pregunta de entrevista de Amazon

Coding related question generally based on tree