Use a stack-based approach instead of a recursive algorithm