Your "out of memory" error is likely caused by an infinite recursion condition. Check that the base case actually exists so that the recursive algorithm has an exit.