How do I exclude the lowest level terms in a taxonomy?

If you have only 4 level, and each third level term has children, the we can determine the fourth level term if have children:

$term_id       = get_queried_object()->term_id;
$taxonomy_name="product_range";
$custom_terms  = get_term_children( $term_id, $taxonomy_name );

foreach ( $custom_terms as $custom_term ) {

    $term          = get_term_by( 'id', $custom_term, $taxonomy_name );
    $terms_chilren = get_term_children( $term_id, $taxonomy_name );

    wp_reset_query();

    // if we can not get a term`s children, we can confirm the term is the fourth level term.
    if ( ! empty( $terms_chilren ) && ! is_wp_error( $terms_chilren ) ) {
       $args = [
        'post_type'      => 'product',
        'posts_per_page' => 1,
        'tax_query'      => [
            [
                'taxonomy' => 'product_range',
                'field'    => 'slug',
                'terms'    => $term->slug,
                'parent'   => 0,
            ],
        ],
    ];
  }
}