Try function RH_STRUC_GET and check the result_objec in case of empty positions.
Try function HRHAP_SEL_MANAGER_OF_HIGH_ORG to find a higher-level manager if the position is empty.
Alternatively use function RH_GET_LEADING_POSITION to get the position ID of the team leader. (For example this is what SAP Workflow rule 157 does.)
A manual approach is to repeatedly search table hrp1001 using the specific relationships from an employee to a position, postiton to an org unit etc. See method /flm/sample=>pernr_get_manager