my @queue = (1);
my %seen;

while (my $id = shift @queue) {
    $seen{$id}++;
    push @queue, grep { !seen{$id} } $self->get_more_ids($id);
}