Python solution of Trees and Graphs part