def find_bricks(top_bricks, predicate):
 found = []
 visited = set()
 to_visit = deque(top_bricks)
 while (len(to_visit) > 0):
  current = to_visit.popleft()
  if (current not in visited):
   visited.add(current)
   if predicate(current):
    found.append(current)
   to_visit.extend(current.children)
 return found

def find_bricks ( top_bricks , predicate ) : found = [ ] visited = set ( ) to_visit = deque ( top_bricks ) while ( len ( to_visit ) > 0 ) : current = to_visit . popleft ( ) if ( current not in visited ) : visited . add ( current ) if predicate ( current ) : found . append ( current ) to_visit . extend ( current . children ) return found
walk the brick hierarchy .