def cut_after(node, levels, removed):
 if (levels == 0):
  removed.extend(node.children)
  node.children = []
 else:
  removed_local = []
  for child in node.children:
   if child.visible:
    cut_after(child, (levels - 1), removed)
   else:
    removed_local.append(child)
  for removed_child in removed_local:
   node.children.remove(removed_child)
  removed.extend(removed_local)

def cut_after ( node , levels , removed ) : if ( levels == 0 ) : removed . extend ( node . children ) node . children = [ ] else : removed_local = [ ] for child in node . children : if child . visible : cut_after ( child , ( levels - 1 ) , removed ) else : removed_local . append ( child ) for removed_child in removed_local : node . children . remove ( removed_child ) removed . extend ( removed_local )
given a tree of nodes cuts after n levels .