Skip to main content

Find Center of Star Graph

EasyLinked ListTreeGraph

Description

Given an undirected star graph (a center node connected to all others), find and return the center of the graph.

Examples

Input:edges = [[1,2],[2,3],[4,2]]
Output:2
Explanation:

Node 2 appears in all edges: [1,2], [2,3], [4,2]. In a star graph, the center connects to every other node.

Input:edges = [[5,7],[6,7],[7,8]]
Output:7
Explanation:

Edges=[[5,7],[6,7],[7,8]]: Check first two edges [5,7] and [6,7]. Node 7 appears in both (position [1] in first edge, position [1] in second edge). Since the center must appear in every edge, node 7 is the center.

Input:edges = [[3,9],[9,1],[9,4],[2,9],[9,10]]
Output:9
Explanation:

Edges=[[3,9],[9,1],[9,4],[2,9],[9,10]]: Check first two edges [3,9] and [9,1]. Node 9 appears in both (position [1] in first, position [0] in second). The center node 9 connects to all leaf nodes: 3, 1, 4, 2, and 10.

Constraints

  • 3 ≤ n ≤ 10⁵

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.