Amazon interview question

Neetcode 150 tree traversal problem