On computing Prüfer Codes and their corresponding trees optimally