added a function to recursively calculate the number of nodes