Amazon interview question

Coding question of tree and graph