easy +10 pts

Find center of star graph

Given an undirected star graph's edges, identify the central node connected to all others.

A **star graph** has exactly one center node connected to every other node, and there are no other edges. You are given a list of `n - 1` edges where `n` is the number of nodes (numbered from 1 to `n`). Each edge is a pair `[u, v]` meaning there is an undirected edge between node `u` and node `v`. The given graph is guaranteed to be a valid star graph. Write a function `find_center(edges)` that returns the center node of the star graph. **Input**: `edges` — a list of integer pairs, each pair `[u, v]` with `1 <= u, v <= 10^5`. The array contains exactly `n - 1` edges for `n >= 3` nodes. **Output**: an integer — the label of the center node. **Note**: A straightforward approach comparing the first two edges is sufficient and correct. Do not assume the edges are sorted.

Constraints

3 ≤ n ≤ 10^5 (number of nodes). edges.length == n - 1. Each edge is a pair [u, v] with distinct nodes. Guaranteed to form a valid star graph. Expected time complexity: O(1) or O(n) — both acceptable; O(1) possible.

Example

>>> find_center([[1,2],[2,3],[4,2]])
2
>>> find_center([[1,2],[5,1],[1,3],[1,4]])
1
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

In a star graph, the center appears in every edge.
Only need to examine the first two edges.
The common node of the first two edges is the center.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.