Consider this tree as example.
2 - 5 - 8 - 6 - 9 - 12 - 20
The easiest way to do so is this:
$category = Mage::getModel('catalog/category')-&gt;load(20);
Each category has a field called path that contains the ids of the parents and current category.
$path = $category-&gt;getPath();
This should return something like this 1/2/6/12. All you need to do is to split this string by / and remove the first element. That one is the id for ‘root of all roots’.
$ids = explode('/', $path); unset($ids);