Amazon Interview Question

medium LC problem on trees