correct infinite recursion bug in toKingdomTree