You can get all ancestors with get_post_ancestors
.
The root ancestor is the last element of the returned array.
Here is the function that checks a target page id against the root ancestor of current page:
function check_page_parent( $target_page_id ) {
$ancestors = get_post_ancestors( get_the_ID() );
if ( $ancestors ) {
$top_most_parent_index = count( $ancestors ) - 1;
$top_most_parent_id = $ancestors[ $top_most_parent_index ];
return ( $top_most_parent_id == $target_page_id);
}
return false;
}