There is an undirected star graph with n nodes labeled 1 to n. A star graph has one center node and exactly n - 1 edges, each joining the center to one of the other nodes. Given the 2D array edges, where edges[i] = [u, v] is an edge between u and v, return the center of the star.
There is an undirected star graph with n nodes labeled 1 to n. A star graph has one center node and exactly n - 1 edges, each joining the center to one of the other nodes. Given the 2D array edges, where edges[i] = [u, v] is an edge between u and v, return the center of the star.
Example 1:
Input: {"edges":[[1,2],[2,3],[4,2]]}
Output: 2
Constraints:
3≤n≤105
edges.length==n−1
edges[i].length==2
1≤ui,vi≤n
ui=vi
The given edges represent a valid star graph.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"edges":[[1,2],[2,3],[4,2]]}
A star with 4 nodes has one hub touching all 3 edges. The hub must appear in every edge, so two edges are enough to find it.