40. Find Center of Star Graph

EasyArrayArray

There is an undirected star graph with n nodes. You are given a 2D integer array edges where each edges[i] = [u, v] represents an edge between nodes u and v. Return the center of the star graph — the node connected to every other node.

Input: A 2D array edges where edges[i]=[u,v] represents an edge.

Output: Integer — the center node of the star graph.

Examples

Example 1
Input: [[1,2],[2,3],[4,2]]
Output: 2
Explanation: Node 2 appears in all edges: [1,2],[2,3],[4,2]. Center=2.
Example 2
Input: [[1,2],[5,1],[1,3],[1,4]]
Output: 1
Explanation: Node 1 appears in all edges. Center=1.
Example 3
Input: [[2,1],[3,2],[4,2],[5,2]]
Output: 2
Explanation: Node 2 connects to all others. Center=2.

Constraints

Asked by

MicrosoftBloombergAmazonGoogle
Solve this problem in the editor →