Explain how you would optimize a recursive algorithm to prevent stack overflow issues in resource-constrained environments.