Solution for Spartez "Flatten a tree" task.