[How to] Identify a root node in a network?

Hello, I have written the following function to get list of nodes.

void getRootId() {
  size_t i=0;
  SimpleList<uint32_t> nl = mesh.getNodeList();
  SimpleList<uint32_t>::iterator itr = nl.begin();
  if(mDebug)
    Serial.print(F("[+] Running checkRoot Task..."));
  while(itr != nl.end()) {
    if(mDebug) {
      Serial.print(F("[+] MeshNode (Chip-ID) -> "));
      Serial.println(*itr,HEX);
    }
    itr++;
  }
}

Here I am able to get the node IDs of connected nodes. However, my requirement is to scan for the root node, if it exists then connect to it, otherwise make this node as a root node. How to do this? Please let me know method to check the node is root or not with nodeID.

Pseudo-code:
Power ON
-> Connect to AP with 'stationManual' if possible
-> else connect to the mesh
-> Scan for the root node (which must be a bridge)
-> if no root found, make this root (only if this is connected to AP using 'stationManual')
-> If existing root drops, make new root if possible