Distinct tree counter for a code golf question