unified tree code (one or more depth levels handled by the same template)