use recursive algorithm to solve the problem