how to get all the last level term id's of taxonomy in drupal 7? i have a  taxonomy 

 State
      - district 1
          -block 1
            -village 1
            -Village 2
          -block 2
          -block 3
      - district 2
      - district 3


How to get all the term id's of all the villages in a state?
&lt;?php
function get_term_depth($tid) {
  $limit = 5; // level of your term 
  $depth = 0; 
  while ($parent = db_select('taxonomy_term_hierarchy', 't') -&gt;condition('tid', $tid, '=') -&gt;fields('t') -&gt;execute() -&gt;fetchAssoc()) {
    $depth ++;
    $tid = $parent['parent'];
    if ($depth &gt; $limit) {
      break;
    }
  }
  return $depth;
}
?&gt;