Amazon Interview Question

a medium level tree problem