#NEWLINE# def findMinHeightTrees( self , n , edges ) : #NEWLINE# #INDENT# '\n        :type n: int\n        :type edges: List[List[int]]\n        :rtype: List[int]\n        ' #NEWLINE# if ( n == 1 ) : #NEWLINE# #INDENT# return [ 0 ] #NEWLINE# #UNINDENT# neighbors = collections . defaultdict ( set ) #NEWLINE# for ( u , v ) in edges : #NEWLINE# #INDENT# neighbors [ u ] . add ( v ) #NEWLINE# neighbors [ v ] . add ( u ) #NEWLINE# #UNINDENT# ( pre_level , unvisited ) = ( [ ] , set ( ) ) #NEWLINE# for i in xrange ( n ) : #NEWLINE# #INDENT# if ( len ( neighbors [ n ] ) == 1 ) : #NEWLINE# #INDENT# pre_level . append ( i ) #NEWLINE# #UNINDENT# unvisited . add ( i ) #NEWLINE# #UNINDENT# while ( len ( unvisited ) > 2 ) : #NEWLINE# #INDENT# cur_level = [ ] #NEWLINE# for u in pre_level : #NEWLINE# #INDENT# unvisited . remove ( u ) #NEWLINE# for v in neighbors [ u ] : #NEWLINE# #INDENT# if ( v in unvisited ) : #NEWLINE# #INDENT# neighbors [ v ] . remove ( u ) #NEWLINE# if ( len ( neighbors [ v ] ) == 1 ) : #NEWLINE# #INDENT# cur_level . append ( v ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# pre_level = cur_level #NEWLINE# #UNINDENT# return list ( unvisited )