Bloomberg interview question

a hard leet code question about graphs/trees