add a recursive solution, O(n) space