Amazon interview question

medium LC problem on trees