Get object terms with no children?

I have written this function some time ago, it may be a bit sloppy but it does it’s job:

function get_low_level_term( $taxonomy ) {
$term = null;

if( is_single() ) {
    global $post;
    $terms = get_the_terms( $post->ID, $taxonomy );

    if( count( $terms ) == 1 ) {
        foreach( $terms as $t ) {
            $term = $t;
        }
    }
    else {
        foreach( $terms as $t ) {
            if( $t->parent ) {
                $term = $t;
            }
        }
        if( !$term ) {
            $count = 0;
            foreach( $terms as $t ) {
                $term = $count == 0 ? $t : $term;
                $count ++;
            }
        }
    }
}

return $term;
}