Amazon interview question

Treee and graph related mainly