solve recursively