replace stack with recursion