public static List<Step<?,?>> getStepsOfAssignableClassRecursivelyFromDepth(final Traversal.Admin<?, ?> traversal, final Class<?>... stepClasses) {
        final List<Step<?,?>> list = new ArrayList<>();
        final Stack<Step<?,?>> stack = new Stack<>();

        traversal.getSteps().forEach(stack::push);

        while (!stack.isEmpty()) {
            final Step<?,?> current = stack.pop();
            list.add(current);

            if (current instanceof TraversalParent) {
                ((TraversalParent) current).getLocalChildren().forEach(localChild -> localChild.getSteps().forEach(stack::push));
                ((TraversalParent) current).getGlobalChildren().forEach(globalChild -> globalChild.getSteps().forEach(stack::push));
            }
        }

        // sort by depth
        list.sort(DepthComparator.instance());

        return list.stream().filter(s -> {
            for (Class<?> stepClass : stepClasses) {
                if (stepClass.isAssignableFrom(s.getClass()))
                    return true;
            }
            return false;
        }).collect(Collectors.toList());
    }
Get steps of the specified classes throughout the traversal, collecting them in a fashion that orders them
from the deepest steps first.
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalHelper.java