Yandex interview question

Algorithmic task on tree traversal