func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   vector<int> adj[N], nodesAtDepth[N];   int degree[N], Dist[N];   bool isDiam[N];   int n;   int bfs(int start) {     queue<pair<int, int>> q;     q.push({start, start});     Dist[start] = 0;     int mxDist = -1;     int mxNode = -1;     while (!q.empty()) {       int node = q.front().first;       int prev = q.front().second;       q.pop();       if (mxDist < Dist[node]) {         mxDist = Dist[node];         mxNode = node;       }       for (int to : adj[node]) {         if (to == prev) continue;         q.push({to, node});         Dist[to] = 1 + Dist[node];       }     }     assert(mxNode != -1);     return mxNode;   }   void dfs(int node, int par, int Depth) {     nodesAtDepth[Depth].push_back(node);     for (int child : adj[node]) {       if (child == par) continue;       dfs(child, node, Depth + 1);     }   }   void solve(int root) {     for (int d = 0; d < n + 3; ++d) {       nodesAtDepth[d].clear();     }     dfs(root, root, 0);     for (int d = 0; d < n + 3; ++d) {       int firstNode = -1;       for (int node : nodesAtDepth[d]) {         if (firstNode == -1) {           firstNode = node;           continue;         }         if (degree[firstNode] != degree[node]) {           return;         }       }     }     cout << root;     exit(0);   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     if (n == 1) {       cout << 1;       return 0;     }     for (int i = 0; i < n - 1; ++i) {       int u, v;       cin >> u >> v;       ++degree[u];       ++degree[v];       adj[u].push_back(v);       adj[v].push_back(u);     }     int end1 = bfs(1);     int end2 = bfs(end1);     int curr = end2;     vector<int> diam;     while (curr != end1) {       diam.push_back(curr);       for (int to : adj[curr]) {         if (Dist[to] == -1 + Dist[curr]) {           curr = to;           break;         }       }     }     diam.push_back(end1);     solve(end1);     solve(end2);     solve(diam[diam.size() / 2]);     queue<pair<int, int>> q;     q.push({diam[diam.size() / 2], diam[diam.size() / 2]});     while (!q.empty()) {       int node = q.front().first;       int par = q.front().second;       q.pop();       if (degree[node] == 1) {         solve(node);         cout << -1;         return 0;       }       for (int child : adj[node]) {         if (child == par) continue;         if (degree[child] > 2) continue;         q.push({child, node});       }     }     cout << -1;   }   