def get_root_of(doctype):
 return frappe.db.sql(u'select t1.name from `tab{0}` t1 where\n DCTB  DCTB (select count(*) from `tab{1}` t2 where\n DCTB  DCTB  DCTB t2.lft < t1.lft and t2.rgt > t1.rgt) = 0'.format(doctype, doctype))[0][0]

def get_root_of ( doctype ) : return frappe . db . sql ( u'select t1.name from `tab{0}` t1 where\n DCTB  DCTB (select count(*) from `tab{1}` t2 where\n DCTB  DCTB  DCTB t2.lft < t1.lft and t2.rgt > t1.rgt) = 0' . format ( doctype , doctype ) ) [ 0 ] [ 0 ]
get root element of a doctype with a tree structure .