Find Center of Star Graph (LeetCode 1791): Solution and Explanation

Easy
Graph

You get the edge list of a star graph, where one hub node is joined to every other node. Return the hub.

Intuition

In a star, every edge has the hub at one end and a leaf at the other. A leaf belongs to exactly one edge, while the hub belongs to all of them.

Pick any two edges. They cannot share a leaf, because each leaf has only one edge. The only node they can share is the hub, and they must share it.

So looking at edges[0] and edges[1] is enough. Whichever end of edges[0] also shows up in edges[1] is the answer, no matter how many nodes the star has.

Approach: Compare the first two edges

  1. Read a, b = edges[0] and c, d = edges[1].
  2. If a == c or a == d, then a is in both edges, so return a.
  3. Otherwise a is a leaf, and the shared node must be b. Return b.

Step-by-step example

edges = [[1,2],[2,3],[4,2]]
  1. edges[0] = [1, 2], so the hub is 1 or 2.
  2. edges[1] = [2, 3]. The hub is in this edge too.
  3. a = 1 is neither 2 nor 3, so 1 is a leaf.
  4. The hub is the other end of edges[0]: return 2. Every edge, including [4, 2], touches 2.

Code: Python, Java, C++

1class Solution:
2    def findCenter(self, edges: List[List[int]]) -> int:
3        a, b = edges[0]
4        c, d = edges[1]
5        if a == c or a == d:
6            return a
7        return b
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Compare the first two edges O(1)
Set intersection of two edges O(1)
Count degrees O(n)
Time

O(1)

Only two edges are read and at most two comparisons are made, whatever the size of the graph.

Space

O(1)

Four integers are stored.

Other approaches

Count degrees (time O(n), space O(n))

Count how many edges touch each node and return the node with degree n - 1. It works on any edge order but reads the whole list and needs a count array.

Set intersection of two edges (time O(1), space O(1))

Put edges[0] in a set and return the end of edges[1] that is in it. Same idea as the comparison, just written with a set.

Common mistakes

  • Assuming the hub is always written first in each pair. The pairs can list the hub at either end.
  • Only checking a == c. The hub can sit at a different position in the second edge, so compare against both c and d.
  • Building a full adjacency list or degree map. It works, but it does O(n) work for an O(1) problem.

Frequently asked questions

Why are two edges enough?

Each leaf is in exactly one edge, so two different edges can only share the hub. Since the hub is in every edge, they always share it.

Does this work if the input is not a star?

No. The trick relies on the guarantee that the graph is a valid star. For a general graph you would count degrees instead.

What if a == c and b == d?

That would be the same edge listed twice, which a valid star graph never contains. In a star the two edges always share exactly one node.