Use ArrayDeque instead of stack.