Display parent page id using child page

<?php

if ($post->post_parent) {

$ancestors=get_post_ancestors($post->ID);

$root=count($ancestors)-1;

$parent = $ancestors[$root];

} else {

$parent = $post->ID;

} ?>