Python Solution of Trees and Graphs part