Amazon interview question

a medium level tree problem