solution for problem 24

use TreeSet and recursion algo