public static <T> void backwardTraverseTree(
    @NotNull T root,
    @NotNull Function<T, Collection<? extends T>> getChildren,
    @NotNull Consumer<T> action
  ) {
    T previous = root;

    Deque<T> stack = new ArrayDeque<>();
    stack.addFirst(root);
    while (!stack.isEmpty()) {
      T current = stack.peekFirst();
      List<? extends T> children = new ArrayList<>(getChildren.apply(current));
      if (children.isEmpty() || children.get(children.size() - 1) == previous) {
        current = stack.removeFirst();
        action.accept(current);
        previous = current;
      }
      else {
        for (int i = children.size() - 1; i >= 0; i--) {
          stack.addFirst(children.get(i));
        }
      }
    }
  }
public static <T> void backwardTraverseTree(
    @NotNull T root,
    @NotNull Function<T, Collection<? extends T>> getChildren,
    @NotNull Consumer<T> action
  )